【问题标题】:How to redirect domain.com or www.domain.com to https www.domain.com如何将 domain.com 或 www.domain.com 重定向到 https www.domain.com
【发布时间】:2017-09-23 20:56:04
【问题描述】:

我想将我的域 http 重定向到 https,并且我已经使用下面提到的代码做到了这一点

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ https www.domain.com/$1 [L,R=301,NC]

当用户输入 domain.com 时,它会自动重定向到 https www.domain.com 但是当用户仅输入 www.domain.com 时,它会重定向到 http www.domain.com

请帮我解决这个问题。

【问题讨论】:

标签: .htaccess cpanel


【解决方案1】:

你可以使用:

RewriteEngine on
# Test without www
RewriteCond %{HTTP_HOST} !^www\. [OR,NC]
# Test for http
RewriteCond %{HTTPS} off 
# Redirect to https www
RewriteRule ^ https://www.exemple.com%{REQUEST_URI} [NE,R=301,L]

【讨论】:

    【解决方案2】:

    在您的代码中:

    RewriteCond %{HTTP_HOST} ^domain.com [NC]
    

    这是以下规则的条件;因此该规则将仅适用于无 www 请求。

    此外,在测试新代码时不要使用R=301 重定向,除非您确保您的规则没问题,并且您可以使用R=302 或只使用R

    不管你的代码如何 将以下代码放在你的主目录.htaccess

    RewriteEngine On
    RewriteCond %{HTTPS} !=on
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
    

    如果你想强制每个请求都是www,你应该这样做:

    RewriteEngine On
    
    RewriteCond %{HTTPS} !=on
    #this line above will match any request without `https`
    
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
    
    #the two lines above will exclude none `www` request and force only `www` request unto `https` and the rule will stop here `L`
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
    
    # the last two line will exclude any `www` request and force only none `www` into `https`
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多