使用 .htaccess 重定向到同一页面
从我的 .htaccess 文件:
RewriteRule ^showPAGE.php page [NC,R=301]
RewriteRule ^page showPAGE.php [NC,L]
我希望用户将访问 url domain.com/showPAGE.php
的用户重定向到 domain.com/page
。 当输入 domain.com/page
时,我希望它显示文件 showPAGE.php
的内容。 这可能吗? 上述结果导致无限重定向循环。
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在尝试做一些非常棘手的事情。问题在于,根据设计,
RedirectRule
指令总是再次触发完整的规则集。只有当您获得的最终 URL 与任何规则都不匹配时,您才能跳出循环,这是棘手的部分,因为您正在重复使用showPAGE.php
名称。到目前为止,我最好的尝试是添加一个假的隐藏字符串:
它有效,但并不令人愉快。毫无疑问,使用 PHP 处理重定向或简单地选择另一个名称会更容易。
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 theshowPAGE.php
name.My best attempt so far involves adding a fake hidden string:
It works but it's not pleasant. Definitively, it's easier to handle the redirection from with PHP or to simply pick another name.
从
showPAGE.php
到page
的重定向需要有 [L],这样它将立即停止处理并重定向,而不是继续并应用其他规则(这在一旦将其映射回 showPAGE.php)。试试这个:The redirect from
showPAGE.php
topage
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: