问题
I've created a bunch of RewriteRules for my website and have had no problem with them on my local setup. Here's a snippet from my .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^news/?$ news.php [L]
Locally, when I visit 127.0.0.1/news, it redirects properly to news.php and masks the URL. I just updated the .htaccess file on the remote server and receive this error, when trying to visit the above example:
The requested URL /mnt/target02/123456/123456/www.mywebsite.com/web/content/news.php was not found on this server.
I've tried changing the rule to this:
RewriteRule ^news/?$ http://www.mywebsite.com/news.php [L]
and the page loads properly. However, the address bar shows news.php, rather than news. Is there something I am missing, or am I stuck with the ugly (and less secure) address? Thanks!
回答1:
If you are like me you are trying to avoid adding leading slashes to your substitution URL (the new destination) so that your same .htaccess
file can work with both your test and production servers. I had the same problem you were having and all that was needed was to set:
RewriteBase /
This tells mod_rewrite to base the substitution URLs off of the web root rather than the file root. Another tip, if you need to redirect to the top level of the destination, use ./
instead of just /
. My test server has a port (8080) and is not at the root (so it has a non-empty RewriteBase
of /site1/
), and most of the rewrite rule examples on the web do not consider this case. If you never have your substitutions start with a leading slash, you can use the same .htaccess
file on your test server and live server, and all you might need to change is the value of RewriteBase
, rather than updating every single rule.
回答2:
Have you tried adding a leading slash to the destination URL e.g.
RewriteRule ^news/?$ /news.php [L]
来源:https://stackoverflow.com/questions/13595803/rewriterule-is-redirecting-to-an-absolute-path-rather-than-the-proper-url