简体   繁体   中英

https Sub-Domain not redirecting correctly with htaccess

I have a domain called: example.com and a subdomain called: sub.example.com

I want to force all primary domain requests for example.com to https:// except for 1 folder. Example:

http://www.example.com/ <-- Redirect all requests to https://
http://www.example.com/folder/ <-- Force to http:// not https://

I also want to force all subdomain requests for sub.example.com to https:// except for 1 folder. Example:

http://sub.example.com <-- Redirect all requests to https://
http://www.example.com/folder/ <-- Force to http:// not https://

Currently example.com is working fine and forces to https:// for the primary domain and the folder is not redirecting to https:// however the folder is not enforcing http:// if someone chooses https://

If i visit http://sub.example.com it does not enforce https:// and if I visit https://sub.example.com manually it shows as such in the url. However the website it displays is actually from https://www.example.com

The htaccess file i have for example.com is:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteCond %{SERVER_PORT} 80 
    RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]        
    RewriteCond %{HTTP_HOST} !^sub.example.com$ [NC]
    RewriteCond %{REQUEST_URI} !^/folder/
    RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,QSA,L]

 </IfModule>

The htaccess file i have for sub.example.com is:

RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} !^/folder/
RewriteCond %{HTTP_HOST} ^example\.com\.au$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

I found the issue. It was unrelated to the htaccess file. I found the certificate i had was installed onto the primary domain and the subdomain did not have a certificate that matched.

Apache was redirecting to the ssl certificates domain instead of the subdomain. I solved this by purchasing a wild card ssl, installing it onto the primary domain and then separately installing it onto the sub domain also.

This fixed the problem.

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