【问题标题】:htaccess redirect domain to https, subdomain to http and www to non-wwwhtaccess 将域重定向到 https,将子域重定向到 http,将 www 重定向到非 www
【发布时间】:2015-05-19 05:48:39
【问题描述】:

我正在尝试这样做:

强制我的主域使用 https。

http or https://www.domain.com  -> https://domain.com
http or https://domain.com  -> https://domain.com

但不适用于子域

http or https://www.subdomain.domain.com -> http://subdomain.domain.com
http or https://subdomain.domain.com -> http://subdomain.domain.com

并且总是删除 www。

现在我有了:

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

这会将 www 重定向到非 www,将 http 重定向到 https,但不适用于子域。子域保留 www 和 https。

谢谢

【问题讨论】:

  • “对我的情况没有用”是什么意思?你想做的没什么特别的,有现有的解决方案。请发布您的尝试并解释为什么它们不适合您。这样我们就可以一起解决您在尝试中必须解决的问题。
  • 您还想“解释为什么他们不适合您”。它做什么,它不做什么?
  • 它们适用于域,但不适用于子域。

标签: .htaccess https no-www


【解决方案1】:

我也使用了以下方法,效果很好。我很难将我的主域 www.domain.com 和 http://domain.com 指向 https://domain.com

非常感谢;因为我已经尝试完成这几个小时了!!!

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

【讨论】:

    【解决方案2】:

    为了得到你想要的组合。您将需要使用您的域。

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

    【讨论】:

      【解决方案3】:

      您可以使用以下两条规则:

      # for main domain
      RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
      RewriteCond %{HTTPS} off [OR]
      RewriteCond %{HTTP_HOST} ^www\. [NC]
      RewriteRule ^ https://domain.com%{REQUEST_URI} [R=301,L,NE]
      
      # for sub domain
      RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com$ [NC]
      RewriteCond %{HTTPS} on [OR]
      RewriteCond %{HTTP_HOST} ^www\. [NC]
      RewriteRule ^ http://subdomain.domain.com%{REQUEST_URI} [R=301,L,NE]
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-06-02
        • 1970-01-01
        • 1970-01-01
        • 2017-11-24
        • 2017-03-24
        • 1970-01-01
        • 2020-05-27
        • 1970-01-01
        相关资源
        最近更新 更多