htaccess - 重定向循环

发布于 2025-01-01 05:56:24 字数 492 浏览 0 评论 0原文

为什么这会导致重定向循环?

Options +FollowSymlinks
RewriteEngine on
RewriteBase /summary/1/document/

RewriteRule !^[a-z]{2}/ /summary/1/document/en/ [L,R]
RewriteRule ^([a-z]{2})/ ../index.php?lang=$1&type=document

我想要实现的是:

如果没有指定语言,则重定向到英语:

示例:

website.com/summary/1/document --> website.com/summary/1/document/en/
website.com/summary/1/document/fr/ [no redirect]

当指定语言时,使用 lang 和类型参数在内部重写为 ../index.php 。

Why is this causing a redirect loop?

Options +FollowSymlinks
RewriteEngine on
RewriteBase /summary/1/document/

RewriteRule !^[a-z]{2}/ /summary/1/document/en/ [L,R]
RewriteRule ^([a-z]{2})/ ../index.php?lang=$1&type=document

What I am trying to achieve is:

If there's no language specified, redirect to english:

Example:

website.com/summary/1/document --> website.com/summary/1/document/en/
website.com/summary/1/document/fr/ [no redirect]

And when a language is specified, rewrite internally to ../index.php with lang and type parameters.

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

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

发布评论

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

评论(1

惜醉颜 2025-01-08 05:56:24

来自标志LApache 文档< /sup>: flag_l :

如果您在 .htaccess 文件或 部分中使用 RewriteRule,那么了解规则的处理方式非常重要。其简化形式是,一旦规则被处理,重写的请求将被交回 URL 解析引擎以执行其可能的处理。当处理重写的请求时,可能会再次遇到.htaccess 文件或部分,因此规则集可能会从头开始再次运行。最常见的是,如果其中一个规则导致重定向(无论是内部还是外部),从而导致请求过程重新开始,就会发生这种情况。


由于重写的请求被交回给 URL 解析引擎,因此在从第一个重写规则重定向后,

尝试以下操作:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /summary/1/document/

RewriteRule ![a-z]{2}/$ /summary/1/document/en/ [NC,L,R]
RewriteRule ^([a-z]{2})/$ ../index.php?lang=$1&type=document [L,QSA]

From Flag L: Apache Docs: flag_l :

If you are using RewriteRule in either .htaccess files or in <Directory> sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.


Since, rewritten request is handed back to the URL parsing engine, after redirect from the first rewrite rule,

Try this:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /summary/1/document/

RewriteRule ![a-z]{2}/$ /summary/1/document/en/ [NC,L,R]
RewriteRule ^([a-z]{2})/$ ../index.php?lang=$1&type=document [L,QSA]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文