.htaccess HTTPS Main Domain & Wildcard HTTP Sub-Do

2019-07-08 22:13发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-07-08 22:56

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]
查看更多
登录 后发表回答