.htaccess HTTPS Main Domain & Wildcard HTTP Sub-Domains + All Non-WWW

我怕爱的太早我们不能终老 提交于 2019-12-23 23:34:39

问题


There are many similar solutions on Stack Overflow such as htaccess http to https with www. Without redirecting sub domain.

What I need, however, is:

  • Main domain HTTPS + NON-WWW
  • Wildcard HTTP for ALL sub-domains instead of adding one after another.

I am running a WordPress Multisite website and have no wildcard SSL.

I am using the following at the moment:

Non-WWW

    RewriteEngine on
    RewriteBase /
    RewriteCond %{HTTP_HOST} www.(.*)$ [NC]
    RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

Non-WWW and Non-HTTPS Sub-Domains

    RewriteEngine On
    RewriteCond %{HTTPS} off

    RewriteCond %{HTTP_HOST} !=subdomain1.main.com
    RewriteCond %{HTTP_HOST} !=subdomain2.main.com
    RewriteCond %{HTTP_HOST} !=subdomain3.main.com
    RewriteCond %{HTTP_HOST} !=subdomain4.main.com

SSL

    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

回答1:


Is this what you want?

RewriteEngine On
RewriteBase /

# www is redirected to base domain
RewriteCond %{HTTP_HOST} ^www\.([^\.]+\.[^\.]+)$
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]

# base domain should use HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [L,R=301]

# other domain should use HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} !^[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [L,R=301]


来源:https://stackoverflow.com/questions/37935805/htaccess-https-main-domain-wildcard-http-sub-domains-all-non-www

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!