【问题标题】:Forcing SSL and WWW using .htaccess使用 .htaccess 强制 SSL 和 WWW
【发布时间】:2023-03-27 05:10:01
【问题描述】:

我想知道.htaccess 中用于在 URL 中强制 SSL 和 WWW 的代码是否正确,因为使用其他代码我通常会得到重定向循环,例如RewriteCond %{HTTPS} !=on 现在它就像一个魅力(可疑)。另外,有没有可能写得更好/更简单?

# Force to SSL
RewriteCond %{HTTP:HTTPS} !1
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]

# Force to WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]  

【问题讨论】:

标签: .htaccess ssl web https


【解决方案1】:

我有同样的问题,我用这个来解决它。效果最好。

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L,NE]

【讨论】:

    【解决方案2】:

    无需填写域。这在任何情况下都会强制 WWWHTTPS。这也处理子文件夹。

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

    【讨论】:

    • 我想我更喜欢这个。
    • 完美可靠地工作。
    【解决方案3】:

    我找到了一个 mod_rewrite 解决方案,它适用于代理服务器和非代理服务器。

    如果您使用 CloudFlare、AWS Elastic Load Balancing、Heroku、OpenShift 或任何其他 Cloud/PaaS 解决方案,并且您在使用正常 HTTPS 重定向时遇到重定向循环,请尝试以下 sn-p。

    RewriteEngine On
    
    # If we receive a forwarded http request from a proxy...
    RewriteCond %{HTTP:X-Forwarded-Proto} =http [OR]
    
    # ...or just a plain old http request directly from the client
    RewriteCond %{HTTP:X-Forwarded-Proto} =""
    RewriteCond %{HTTPS} !=on
    
    # Redirect to https version
    RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
    

    【讨论】:

    • 我已经折磨了自己一周,试图让 SSL 在 CloudFlare 后面的主机上正常工作。您的代码运行良好...
    【解决方案4】:

    很抱歉碰到这个话题,但我只是想为搜索引擎访问者添加一个简单的解决方案。

    RewriteEngine on
    # Force WWW & SSL
    RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
    RewriteRule ^(.*)$ https://www.stackoverflow.com/$1 [L,R=301] 
    

    【讨论】:

      【解决方案5】:

      9 Force www: 完美,谢谢。

      我的服务器是 Heart Internet,Heart 的强制 SSL 是:

      # All calls go to SSL
      RewriteEngine On
      RewriteCond %{ENV:HTTPS} !=on
      RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
      

      【讨论】:

        【解决方案6】:

        这有点简单。

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

        【讨论】:

        • 你能解释一下条件吗?我在这里有点新手。
        • 第 2 行检查 HTTPS 是否关闭,然后我们与第 3 行有一个 OR 条件。第 3 行检查主机名是否与 www.domain.com 不同。如果其中一个为真,最后一行将采取行动并将 HTTP 代码 301 的“R=301”重定向到domain.com
        • 由此所有子域也被重定向。例如。 api.domain.com 更改为www.domain.com.. 有没有办法将www 仅添加到根domain.com 而不是任何子域。
        • 这不会将https://example.com 重定向到https://www.example.com
        • 如果您在子文件夹上有另一个 .htaccess,请不要认为这适用于子文件夹
        【解决方案7】:

        使用这个:

        RewriteEngine on
        
        # Force www: from http://stackoverflow.com/a/4958847/1078583
        RewriteCond %{HTTP_HOST} !^$
        RewriteCond %{HTTP_HOST} !^www\. [NC]
        RewriteCond %{HTTPS}s ^on(s)|
        RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
        
        # Force SSL: From http://stackoverflow.com/q/24322035/
        RewriteCond %{HTTPS} off
        RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
        

        【讨论】:

        • 是否有理由将 SSL 重定向设为临时而非永久?
        • @Jabari 看起来我可能已经粘贴了这两段代码(请参阅 URL)。但一般情况下,建议在测试时使用 302,然后当您确定重定向有效时,移至 301。
        猜你喜欢
        • 2013-01-19
        • 1970-01-01
        • 2012-07-30
        • 1970-01-01
        • 2013-05-31
        • 2014-06-19
        • 2018-12-22
        • 1970-01-01
        • 2018-05-23
        相关资源
        最近更新 更多