2012-07-30 49 views
1

我有一个简单的htaccess重定向来将访问者从website.com/promotions发送到website.com/go/promotions。如果我使用下面的代码我得到一个重定向循环错误:.htaccess重定向循环和正则表达式问题

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

的的RewriteCond应该由如果请求网址中包含“走出去”,但它似乎并没有被工作没有进行重定向,以防止循环。

此外,我试图设置一个正则表达式,以便promotion/anypage.html将被重定向到/ promotion/anypage.html。我试着下面的代码,但它不会匹配任何东西:

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

没有任何人有一个线索,为什么这些块的任何工作不正常?

干杯

回答

2

%{REQUEST_URL}不是一个变种可以对阵,你”可能考虑%{REQUEST_URI},但它以一个前导斜杠开头,所以^go永远不会匹配任何东西。

你只需要一个单一的规则:

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

或者你可以使用mod_alias中:

Redirect /promotion/ /go/promotion/ 
+0

非常好。就是这样。看起来我接近我的第二条规则,在报告开始时我只是错过了正斜杠。 – khendar 2012-07-30 23:45:25

1

你检查,如果整个URL仅仅是“走出去”在它自己的,它不是:

RewriteEngine on 
RewriteCond %{REQUEST_URI} !^/?go/ 
RewriteRule promotion/index.html go/promotion/index.html [R,L,NC] 
+0

这并不妨碍循环 – 2012-07-30 08:39:14

+0

是的,我仍然是一个得到一个重定向循环错误。 – khendar 2012-07-30 23:47:46