开发者

Redirecting to same page with .htaccess

From my .htaccess file:

RewriteRule ^showPAGE.php page [NC,R=301]
RewriteRule开发者_开发百科 ^page showPAGE.php [NC,L]

I want users going to url domain.com/showPAGE.php to be redirected to domain.com/page . When domain.com/page is being entered, I want it to show the content of the file showPAGE.php. Is that possible to do? The above results an infinite redirection loop.

Thanks


You're trying to do something that's very tricky. The problem is that, by design, the RedirectRule directive always triggers again the complete set of rules. You can only get out of the loop when you obtain a final URL that does not match any of the rules and that's the tricky part since you are reusing the showPAGE.php name.

My best attempt so far involves adding a fake hidden string:

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/showPAGE\.php
RewriteCond %{QUERY_STRING} !^internal
RewriteRule ^ http://%{HTTP_HOST}/page [NC,R=301,L]

RewriteRule ^page$ showPAGE.php?internal [NC,L]

It works but it's not pleasant. Definitively, it's easier to handle the redirection from with PHP or to simply pick another name.


The redirect from showPAGE.php to page needs to have [L] so that it will stop processing and redirect at once, rather than going on and applying other rules (which at once map it back to showPAGE.php). Try this:

RewriteRule ^showPAGE.php page [NC,R=301,L]
RewriteRule ^page showPAGE.php [NC,L]
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜