【问题标题】:htaccess - rewrite & redirect without a causing a loophtaccess - 重写和重定向而不会导致循环
【发布时间】:2012-11-13 11:18:04
【问题描述】:

旧网站:
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
...这不会导致无限循环?

【问题讨论】:

  • 为什么不能为评估 HTTP_HOST 的规则使用额外的 RewriteCond?
  • @arkascha 我是一个重写假人 - 你能告诉我如何做到这一点吗?

标签: .htaccess redirect rewrite


【解决方案1】:

我怎样才能添加类似这样的重定向:
重定向 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]

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多