开发者

mod_rewrite is redirecting the browser instead of rewriting

I am using the following mod_rewrite to redirect from the top directory of my 开发者_StackOverflowsite, to the subdirectory shows/:

Options +FollowSymlinks
RewriteEngine On
RewriteRule ^$ shows [L]

The redirect is performing fine. However, the URL is displaying the redirect, which I believe it should not if you use mod_rewrite.

To clarify: A browser pointed towards http://www.example.com/ is redirected to the subdirectory shows/. But the browser displays the redirect in the URL as http://www.example.com/shows/. Again, it is my understanding that by using mod-rewrite, you make the redirect invisible, so the user is not aware the redirect has taken place.

Am I doing something wrong?


I now see the problem. You are rewriting a URL from http://www.example.com/ to http://www.example.com/shows. Since /shows is a directory and not a file, Apache sends a trailing slash redirect after rewriting. Here is How you fix it:

RewriteRule ^$ shows/ [L]
# here -------------^

Note: since Apache might have sent a 301 Moved Permanently header earlier, browsers will cache this response and it might show you wrong page/content even after you make changes to your .htaccess file. Clear browser cache often when you're testing.


I was wondering the same for a long time ago,so I decided to search on google and I found your question. I realised that there is something like two options "Redirect" and "Remapping". So I think here is the key to understand what you were asking. Basically, there are two options

redirection without change the url

RewriteEngine on
RewriteRule ^/foo\.html$ /bar.html [PT]

& and redirection changing the url on the browser.

RewriteEngine on
RewriteRule ^/foo\.html$ bar.html [R]

Anyway, take a look here http://httpd.apache.org/docs/2.2/rewrite/remapping.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜