htaccess - 重写&重定向而不会导致循环

时间:2012-11-13 11:18:04

标签: .htaccess redirect rewrite

旧网站:
www.example.com(默认)

新网站
example.com(默认)
和一种新的语言..
es.example.com

目前在旧网站上:
www.example.com/this-page.php

通缉新网站(干净网址):
example.com/this-page
es.example.com/esta-pagina

到目前为止我有这个:

RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

RewriteRule ^this-page/?$ this-page\.php?lang=$1
RewriteRule ^esta-pagina/?$ this-page\.php?lang=$1


** $ _GET ['lang']是内部生成的,具体取决于是否触发了example.com或es.example.com。

问题:

我怎样才能添加类似于此的重定向:
重定向301 /this-page.php http://example.com/this-page
......那不会导致无限循环?

2 个答案:

答案 0 :(得分:2)

  

我怎样才能添加类似于此的重定向:
  重定向301 /this-page.php http://example.com/this-page
  ......这不会导致无限循环?

您需要匹配实际请求而不是URI。当重写引擎循环执行规则时,URI将发生变化。

RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /this-page\.php(\?|\ )
RewriteRule ^ /this-page [L,R=301]

答案 1 :(得分:1)

另外值得注意的是,如果你使用像CloudFlare这样的CDN,%{HTTPS}检查会给你带来无限循环错误。如果您是CloudFlare用户,则需要将RewriteCond%{HTTPS}!=替换为:

RewriteCond %{HTTP:X-Forwarded-Proto} =http