使用 .htaccess 重定向到同一页面

发布于 2024-10-02 07:08:38 字数 335 浏览 0 评论 0原文

从我的 .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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

羁绊已千年 2024-10-09 07:08:38

您正在尝试做一些非常棘手的事情。问题在于,根据设计,RedirectRule 指令总是再次触发完整的规则集。只有当您获得的最终 URL 与任何规则都不匹配时,您才能跳出循环,这是棘手的部分,因为您正在重复使用 showPAGE.php 名称。

到目前为止,我最好的尝试是添加一个假的隐藏字符串:

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]

它有效,但并不令人愉快。毫无疑问,使用 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 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.

鲜肉鲜肉永远不皱 2024-10-09 07:08:38

showPAGE.phppage 的重定向需要有 [L],这样它将立即停止处理并重定向,而不是继续并应用其他规则(这在一旦将其映射回 showPAGE.php)。试试这个:

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

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]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文