【问题标题】:redirect www to non-www while preserving http or https将 www 重定向到非 www,同时保留 http 或 https
【发布时间】:2014-02-24 20:04:04
【问题描述】:

这是一个几乎相同问题htaccess for redirecting non-www to www while preserving http & https的链接

相反的情况如何写 - 我想在保留 http 或 https 的同时将 www 重定向到非 www,无论链接最初可能是什么

【问题讨论】:

    标签: .htaccess http-redirect


    【解决方案1】:

    你可以有这两条规则:

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

    【讨论】:

    • 如果它不能正常工作,你可能需要对 %{HTTPS} 的值做一些实验——可能是 0 和 1,或者 http 和 https 等等。我已经听说服务器之间的 HTTPS 环境变量不同,所以也许这也是。
    • @user3348276:如果它对您有用,您可以通过单击我的答案左上角的勾号将答案标记为已接受。
    【解决方案2】:

    经过一些试验和错误,这是有效的:

    # Make all http use https and force non-www
    <IfModule mod_rewrite.c>
    RewriteCond %{HTTP_HOST} ^www\.
    RewriteRule ^(.*)$ https://mysite.ca%{REQUEST_URI} [L,R=301]
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    </IfModule>
    

    抱歉没有早点回来发帖,我正在学习...

    【讨论】:

      猜你喜欢
      • 2011-09-25
      • 2018-09-26
      • 1970-01-01
      • 1970-01-01
      • 2018-02-02
      • 1970-01-01
      • 2017-11-24
      • 2017-03-24
      • 1970-01-01
      相关资源
      最近更新 更多