【问题标题】:Redirecting https to http as well as domain.com to www.domain.com将 https 重定向到 http 以及将 domain.com 重定向到 www.domain.com
【发布时间】:2012-08-22 06:48:57
【问题描述】:

我正在尝试做一些重定向,其中 https 重定向到 http 以及输入 domain.com 的任何实例都重定向到 www.domain.com。

一个实际的例子是 https://domain.com 被重定向到 http://www.domain.com - 下面是我在 .htaccess 文件中使用的正则表达式代码。非常感谢您帮助澄清。

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

【问题讨论】:

    标签: regex .htaccess redirect http-status-code-301


    【解决方案1】:

    你在正确的轨道上,只需要稍微调整你的规则:

    RewriteEngine on
    
    # we handle to domain.com to www.domain.com first
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    
    # then we redirect anything with HTTPS to just HTTP
    RewriteCond %{HTTPS} on
    RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

      【解决方案2】:

      这适用于 HTTPS

      RewriteEngine On 
      RewriteCond %{HTTPS} off 
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
      

      【讨论】:

      • 这不是 OP 所要求的
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-26
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      相关资源
      最近更新 更多