【问题标题】:htaccess - need to redirect one non https domain to https when multiple domains pointing to the same serverhtaccess - 当多个域指向同一服务器时,需要将一个非 https 域重定向到 https
【发布时间】:2020-02-12 07:47:22
【问题描述】:

当多个域指向同一个服务器时,需要将一个非https域重定向到https。 具体来说,所有非 www 域都需要重定向到 www,并且只有一个域需要重定向到 www 以及 https。

例如,具有 SSL 的域

http://example.com to https://www.example.com
http://www.example.com to https://www.example.com
https://example.com to https://www.example.com

域没有 SSL

http://example2.com to http://www.example2.com
http://example3.com to http://www.example3.com

我已经尝试了许多 stackoverflow 上提供的解决方案,例如 this

我当前的 htaccess 如下所示,

#redirect to https - domain that having SSL
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

#all other non www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

除了一种情况外,一切正常。当我使用具有 SSL 的 www (http://www.example.com) 进入域时,不重定向到 https。

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    如果我确实正确地遵循了您的要求,只需先添加以下内容:

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

    【讨论】:

    • @ArunA 具体用什么网址?
    • 顺便说一句,除非您出于某种原因需要 302 重定向,否则您需要确保所有 [R] 都是 [R=301]。
    猜你喜欢
    • 2020-06-14
    • 2017-11-03
    • 1970-01-01
    • 1970-01-01
    • 2013-04-01
    • 1970-01-01
    • 2016-08-12
    • 2021-04-23
    • 2016-02-25
    相关资源
    最近更新 更多