Delete get variables and .php using htaccess

2019-06-03 01:01发布

I'm trying to solve some htaccess question and I'm not able to reach the solution. I have the following html:

<a href="www.site.com/home.php?Idi="English">English</a> <a href="www.site.com/home.php?Idi="Française">Française</a>

and the goal is that user just gets in its address bar:

www.site.com/home/English

or

www.site.com/home/Française

Obviously, the get variables should arrive to home.php to be processed.

Also, the site has other pages with extension .php and I got delete each extension with:

Options +FollowSymLinks RewriteEngine on RewriteBase / RewriteCond %{REQUEST_FILENAME}.php -f RewriteRule ^ %{REQUEST_URI}.php [L]

Further, I would like the starting page:

www.site.com/home.php

would show as:

www.site.com/home/English

I have seen a lot of tutorials and posts concerning it, but I am not able to solve it.

Any help would be appreciated, thanks

EDITED:

Finally, as UnskilledFreak suggested me, I changed the original links to:

<a href="www.site.com/home/English">English</a>
<a href="www.site.com/home/Française">Française</a>

and the htaccess is as:

Options +FollowSymLinks
RewriteEngine on
RewriteBase /
#Receive site.com/home/Language and internally processes site.com/home.php?Idi=Language
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^home/(English)/?$ home.php?Idi=English [QSA,NC,L]
RewriteRule ^home/(Française)/?$ home.php?Idi=Française [QSA,NC,L]
#Receive site.com/something and internally goes to site.com/something.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

To avoid the crash of css, js and other links loaded in new pages, I added a / before every path of the html.

The htaccess is placed in the root. The 'home' folder doesn't exist in the server.

1条回答
疯言疯语
2楼-- · 2019-06-03 01:28

Like the comments explain AND the edited Post above, it'll work ;)

but here is another solution i get and works too:

in my document_root i've added a folder called test, in there i've got 4 files with following content:

.htaccess

Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /test/index.php [L]

index.php - a wrapper for rewrite and testlinks

echo'
<!DOCTYPE html>
    <head>
        <title>Rewrite Test</title>
        <link rel="stylesheet" type="text/css" href="/test/style.css">
    </head>
    <body>
        <a href="/test/home/english">English</a><br>
        <a href="/test/home/francaise">Francaise</a><br>
';
$get = explode("/",$_SERVER["REQUEST_URI"]);
$get = array_slice($get,2); // ignoring first empty key and folder 'test'

#echo "<pre>".print_r($get,true)."</pre>";

if(file_exists($get[0].".php"))
    require($get[0].".php");
else
    echo 'file '.$get[0].'not found';    

echo'
    </body>
</html>';

home.php - as requested

<?php
echo 'in home.php, you have choosen:'.$get[1];
?>

style.css - only as test that file access will work

body {
    color:#ff8800;
    font-weight:bold:
}

tested and works as expected :)

查看更多
登录 后发表回答