简体   繁体   中英

Wordpress multisite Force HTTPS and WWW for domain and only HTTPS for subdomains HTACESS

I currently have a wordpress multisite setup of that consist of two sites, one is a subdomain. lets call the main domain www.domain.com and the sub domain test.domain.com

  • For both domains I need to force https
  • For the main domain I also need to enforce both ssl and www so that it becomes https://www.domain.com

The main issue I am having is when I enter https://test.domain.com this will work correctly but when I type in http://test.domain.com I get redirected to https://www.domain.com which is the main website and not the subdomain website.

<IfModule mod_rewrite.c>
RewriteEngine On

# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(test\.domain\.co)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

# for main domains
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} ^domain\.co$ [NC]
RewriteRule ^.*$ https://www.domain.co%{REQUEST_URI} [R,L]


RewriteBase /
RewriteRule ^index\.php$ - [L]

# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</IfModule>

For the subdomain, you can use this rule :

# for subdomains
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(test\.example\.com)$ [NC]
RewriteRule ^.*$ https://%1%{REQUEST_URI} [R,L]

Ok so I found the reason why I always redirected to the https version. I had originally setup the apache server to use a virtual hosts. In that file for all http request or rather port 80 eg <VirtualHost *:80> I had stupidly created a redirect rule of Redirect permanent / https://maindomain.co/

I resolved this issue by removing that redirect and letting my .htaccess file manage the redirects. It now works beautifully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM