Using apache/.htaccess RewriteEngine:
If I want to change a url like
www.foo.com/mypage.php?var=abc开发者_如何学运维&var2=123
to
www.foo.com/abc/123
Do I need to rewrite my URLs in my PHP files manually, or is that also handled?
i.e., change in my code <a href="mypage.php?var=abc&var2=123"> to <a href="abc/123">
Does mod_rewrite handle this for me? Or does it only translate the URL it receives, and not actually rewrite generated HTML based on the rules in the .htaccess file?
If you have server httpd configuration access, You can Place the following code in your server apache server config or place into your httaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /mypage.php [L]
</IfModule>
and place the following code in your mypage.php file top,
$Get_data = explode('/', $_SERVER[REQUEST_URI]);
$_GET['var'] = $Get_data[1];
$_GET['var2'] = $Get_data[2];
Now you got same result when you are using these www.foo.com/mypage.php?var=abc&var2=123 url or these www.foo.com/abc/123 url.
Any comments, please let me know.
Yes you have to change it.
Mod rewrite only internally redirects requests internally. It changse the HTTP header that is visible to your application, but you will still be able to reproduce the original url by additional header fields that are ardded. It does not modify any content you send.
精彩评论