Kunwarbir S.
Kunwarbir S.

Reputation: 281

.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]

Upvotes: 1

Views: 445

Answers (2)

anubhava
anubhava

Reputation: 785156

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]

Upvotes: 1

cormpadre
cormpadre

Reputation: 127

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.

Upvotes: 0

Related Questions