【问题标题】:mod_rewrite www to no-www and http to https simultaneouslymod_rewrite www 到 no-www 和 http 到 https 同时
【发布时间】:2015-07-22 16:13:13
【问题描述】:

我目前有 mod_rewrite 代码,我发现可以将所有请求重定向到相同的 URL,而不需要 www。

我现在要做的是将 Apache mod_rewrite 配置为执行以下操作,而不会浪费任何操作/额外的重定向。

http:// to https://

http://www. to https://

我不熟悉它,但在伪代码中,http(s)://(www.)domain.comhttps://domain.com 仅用于顶级 URL,不包括子域。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    你只需要一个规则来满足这两种情况:

    将此代码放入您的DOCUMENT_ROOT/.htaccess 文件中:

    RewriteEngine On
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} ^www\. [NC]
    RewriteRule ^ https://domain.com%{REQUEST_URI} [NE,R=301,L]
    

    【讨论】:

    • +1,我注意到为了保留您使用%{REQUEST_URI} 的网址,使用NE 标志,我喜欢这样。 :)
    【解决方案2】:

    是的,有一种方法可以将这些操作组合成一条规则:

    RewriteCond %{HTTP_HOST} !=example.com [OR]
    RewriteCond %{HTTPS} !=on
    RewriteRule ^/(.*) https://example.com/$1
    

    【讨论】:

      【解决方案3】:

      这个对我有用。我尝试了上面的那个,但它在旧浏览器上创建了一个重定向循环。

      RewriteCond %{SERVER_PORT} !^443$ 
      RewriteRule ^(.*)$ https://example.com/$1 [R=301,L] 
      
      RewriteCond %{SERVER_PORT} ^443$ 
      RewriteCond %{HTTP_HOST} !^example\.com$ [NC] 
      RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-22
        • 1970-01-01
        • 2017-12-28
        • 2021-03-25
        • 2014-12-16
        相关资源
        最近更新 更多