Redirect 和 RewriteRule 一起使用时的 .htaccess 重定向循环

发布于 2025-01-01 03:17:28 字数 836 浏览 1 评论 0原文

我正在 HTACCESS 文件中尝试以下操作:

我想 301 重定向此 --> http://www.domain.com/somepage.php?page=foo

对此——> http://www.domain.com/my-pretty-url/

这有效当我改变网站上的内部链接以阅读我想要的方式并且我不使用 R=301 标志时很好:

RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]

但是...这里的问题是我还想将任何外部请求 301 重定向到服务器,当我处理时它让我陷入重定向循环。

RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]

RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]

RewriteCond 规则本身不起作用,只有底部的单个 RewriteRule 本身可以用于内部重写,但它不处理外部请求。

显然,如果我将两者放在一起,就会形成一个循环。我该如何解决这个问题?

谢谢!

I'm attempting the following in an HTACCESS file:

I want to 301 redirect this --> http://www.domain.com/somepage.php?page=foo

to this --> http://www.domain.com/my-pretty-url/

This works fine when I alter internal links on the site to read how I want and I DO NOT use the R=301 flag:

RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]

BUT... the hitch here is I also want to 301 Redirect any external requests to the server, which when I handle that it puts me in a redirect loop.

RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]

RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]

The RewriteCond rules don't work by themselves, only the single RewriteRule at the bottom works by itself for internal rewrites, but it doesn't handle outside requests.

Obviously, if I have both together, it's creating a loop. How do I get around this??

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

晨与橙与城 2025-01-08 03:17:28

请尝试以下方法来防止循环

#prevent internal redirects, and prevent loop
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]

RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]

Try the following to prevent the looping

#prevent internal redirects, and prevent loop
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{REQUEST_URI} /index.php$
RewriteCond %{QUERY_STRING} ^page=foo$
RewriteRule ^.*$ http://www.domain.com/my-pretty-url/? [R=301,L]

RewriteRule ^my-pretty-url/$ /index\.php?page=foo [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文