简体   繁体   中英

.htaccess rule to force https to root directory and specific sub domain

I want to force https to root directory and a specific subdomain only BUT removing www from every domain.

The following condition removes www from all domain and redirects all to https . Whereas I need https for only root and a subdomain demo

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

You will need more than one rewrite rule here:

# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]

# if https is off
RewriteCond %{HTTPS} off
# a specific subdomain demo
RewriteCond %{HTTP_HOST} ^demo\.domain\.com$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

# if https is off
RewriteCond %{HTTPS} off
# main domain
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^/?$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

right now looks to me your rule says something like... If the http_host has www. rewrite to be without www and with https.

I think you need to split the rules up to rules like. If the http_host has www. remove www if request_uri is the root add https.

I think you are close but i think you need multiple rules not just a single conditional.

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