简单的301重定向可变不能正常工作,为什么?不能正常、重定向、简单、工作

由网友(佡女味の小野)分享简介:下面是我得到了这么远。第一部分作品,但不是重定向本身。 Here's what I got so far. The first part works but not the redirect itself. 什么我需要做的,使其工作?What do I need to do to make it work?Re...

下面是我得到了这么远。第一部分作品,但不是重定向本身。

Here's what I got so far. The first part works but not the redirect itself.

什么我需要做的,使其工作?

What do I need to do to make it work?

RewriteEngine On
RewriteRule ^([^/.]+)/?$ page.php?name=$1 [L]

RewriteRule ^page.php?name=([^/.]+)/?$ /$1  [R=301,L] 

另外,如果我有多个这些规则做我离开 [L] 仅在最后一个?

推荐答案

除了第一条规则重写第二个,第二个规则也不会工作,因为你试图匹配一个重写规则的查询字符串。尝试像这样:

Besides the first rule overriding the second one, your second rule also won't work because you're trying to match the query string in a RewriteRule. Try something like this instead:

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^name=([^/.&]+)/?$
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^page.php$ /%1?  [NS,R=301,L]

RewriteRule ^([^/.]+)/?$ page.php?name=$1 [NS,QSA,E=LOOP:1]

(我包括 QSA 标志,以便像 / foobar的?富=酒吧将被改写为 /page.php?name=的网址foob​​ar的&放大器;富=酒吧,而不是仅仅 /page.php?name=foobar 如果你不希望出现这种情况,离开它)。

(I included the QSA flag so that an URL like /foobar?foo=bar will be rewritten to /page.php?name=foobar&foo=bar instead of just /page.php?name=foobar. If you don't want that, leave it out.)

注意:第二个的RewriteCond 也可把第一个规则的再次匹配第二个匹配了之后。问题在于,在.htaccess的背景下,mod_rewrite的作用或多或少仿佛的所有的规则有的 PT 标志,导致规则集从一开始每个重写,甚至内部的人后,重新运行。或者,引用文档:

Note: The second RewriteCond is there to keep the first rule from matching again after the second one has matched. The problem is that, in .htaccess context, mod_rewrite acts more or less as if all rules had the PT flag, causing the ruleset to be rerun from the start after every rewrite, even internal ones. Or, to quote the documentation:

如果你在任何.htaccess文件或与&lt使用重写规则;目录>的部分,有怎样的规则的处理有一定的了解是非常重要的这样做的简化形式是,一旦该规则已被处理,所重写的请求被交还给URL解析引擎做它可以与它有可能是作为所重写的请求的处理,.htaccess文件或所述;指南>部分可以再次遇到,因此规则集可从一开始就再次运行最常见的会出现这种情况,如果有一条规则导致重定向 - 无论是内部还是外部 - 导致请求过程从头再来

"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 <Directory> 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."

我正在使用的解决方法是设置一个自定义的环境变量 E = LOOP:1 当内部重写触发器,并做外部重写之前检查它。需要注意的是,当内部重写后请求处理重新启动,阿帕奇prepends 重定向_ 至previous通过程中设置的所有环境变量的名称,所以即使尽管我们设置变量命名为刚 LOOP ,我们需要检查是否有一个是 REDIRECT_LOOP

The workaround I'm using is to set a custom environment variable with E=LOOP:1 when the internal rewrite triggers, and check for it before doing the external rewrite. Note that, when the request processing restarts after the internal rewrite, Apache prepends REDIRECT_ to the names of all environment variables set during the previous pass, so even though the variable we set is named just LOOP, the one we need to check for is REDIRECT_LOOP.

阅读全文

相关推荐

最新文章