【问题标题】:htaccess redirect domain to https, subdomain to https and non-www to wwwhtaccess 将域重定向到 https,将子域重定向到 https,将非 www 重定向到 www
【发布时间】:2016-06-03 12:53:04
【问题描述】:

我希望这样做:

强制我的主域使用 https。

http 到https://wwwhttp://wwwhttps://www

但不适用于子域

http://subdomain.domain.comhttps://subdomain.domain.com

谁能帮帮我,我找不到这个

【问题讨论】:

    标签: .htaccess redirect


    【解决方案1】:

    你可以在你的根.htaccess中使用它:

    RewriteEngine on 
    
    # redirect to https www
    RewriteCond %{HTTPS} off
    RewriteCond %{HTTP_HOST} ^(?:www\.)(domain\.com)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
    RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
    
    # redirect to http subdomain
    RewriteCond %{HTTPS} on
    RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC]
    RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
    

    [编辑]

    RewriteEngine on 
    
    # redirect no-www to www only main domain, not with subdomain
    RewriteCond %{HTTP_HOST} ^(domain\.com)$ [NC]
    RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
    
    # redirect http to https all domain
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
    

    【讨论】:

    • 如果我在下面更改子域是否会重定向 https # 重定向到 http 子域 RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^((?!www).+\.domain\.com)$ [NC] RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L] to # 重定向到 https 子域 RewriteCond %{HTTPS} 关闭 RewriteCond %{HTTP_HOST} ^((?!www).+\. domain\.com)$ [NC] RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
    • 好的,知道了。我已经更新了我的答案。您可以使用 EDIT 部分
    • 如果我们需要从 url 中删除 index.php 怎么办?
    • 帮助很大
    猜你喜欢
    • 2015-05-19
    • 2018-06-02
    • 1970-01-01
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-21
    • 2017-11-24
    • 2017-03-24
    相关资源
    最近更新 更多