1

我有一个简单的 htaccess 重定向,可以将访问者从 website.com/promotions 发送到 website.com/go/promotions。如果我使用下面的代码,我会收到重定向循环错误:

RewriteEngine on
RewriteCond %{REQUEST_URL} !^go$
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC] 

如果请求 url 包含“go”但它似乎不起作用,则 RewriteCond 应该通过不重定向来防止循环。

此外,我正在尝试设置一个正则表达式,以便将promotion/anypage.html 重定向到go/promotion/anypage.html。我尝试了以下代码,但它不匹配任何内容:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^go$
RewriteRule ^promotion/(.*)$   go/promotion/$1   [R,L,NC]

有没有人知道为什么这些块中的任何一个都不能正常工作?

干杯

4

2 回答 2

2

%{REQUEST_URL}不是您可以匹配的 var,您可能会想到%{REQUEST_URI},但它以斜杠开头,因此^go永远不会匹配任何内容。

你只需要一个规则:

RewriteEngine On
RewriteRule ^/?promotion/(.*)$ /go/promotion/$1 [R,L]

或者你可以使用 mod_alias:

Redirect /promotion/ /go/promotion/
于 2012-07-30T08:35:50.083 回答
1

您正在检查整个 URL 是否只是它自己的“go”,它不是:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/?go/
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC] 
于 2012-07-30T08:31:33.197 回答