【问题标题】:Using .htaccess to redirect www URLs to non-www for https使用 .htaccess 将 www URL 重定向到 https 的非 www
【发布时间】:2012-02-06 22:45:57
【问题描述】:

请为我提供 .htaccess 语法以执行以下操作:

  1. http 重定向到 https
  2. https://www.domain.com.uk 重定向到 https://domain.com.uk

我尝试了以下方法,但没有成功:

RewriteCond %{HTTP_HOST} ^www.(.*)

重写规则 ^.*$ https://%1/$1 [R=301,L]

【问题讨论】:

    标签: .htaccess redirect ssl


    【解决方案1】:

    尝试将以下内容添加到域根文件夹中的 htaccess 文件中。

    RewriteEngine on
    RewriteBase /
    
    #if not domain.com.uk then redirect to domaim.com.uk
    RewriteCond %{HTTP_HOST} !^domain\.com\.uk$ [NC]
    RewriteRule .* http://domain.com.uk%{REQUEST_URI} [L,R=301]
    
    #if not https
    RewriteCond %{HTTPS} off
    #redirect to https
    RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 它可以正常将 http 重定向到 https 但它没有从 www.domain.com.uk 重定向到 domain.com.uk
    • @RamyAllam 您的 .htacccess 文件中还有其他规则吗?规则是否完全按照上面的顺序排列?如果其中任何一个不同,则可能会影响结果
    • 我在 .htaccess 中没有任何其他规则,我用相同的语法添加了它。我还尝试将它直接放入 httpd.conf RewriteBase 上的 RewriteEngine / #如果不是 domain.com.uk 然后重定向到 domain.com.uk RewriteCond %{HTTP_HOST} !^domain\.com\.uk$ [NC] RewriteRule .* domain.com.uk%{REQUEST_URI} [ L,R=301] #if not https RewriteCond %{HTTPS} off #redirect to https RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] 它没有也不行。
    • @RamyAllam 看起来您在重定向中缺少http://,即它应该是RewriteRule .* http://domain.com.uk%{REQUEST_URI} [L,R=301]。没有它就行不通。
    • 很抱歉,我没有理解您的最后评论。我希望能给我更多细节或输入完整的语法谢谢
    【解决方案2】:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^(www.)?domain.com.uk
    RewriteCond %{HTTPS} off
    RewriteRule ^(.*)$ https://domain.com.uk/$1 [R=301,L]
    

    对我来说,这项工作刚刚经过测试。所以http://www.domain.com.uk into https://domain.com.uk

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2014-12-16
      • 2017-05-03
      • 2018-07-14
      • 1970-01-01
      • 1970-01-01
      • 2016-12-26
      相关资源
      最近更新 更多