.htaccess - www is not HTTPS

Merlin

Expert Member
Joined
Jan 18, 2006
Messages
2,789
Reaction score
320
Location
Cape Town, South Africa
Hi everyone,



I spent many, many hours struggling with this last night...

My host has just instituted Let's Encrypt, so I've edited all of my .htaccess files in order to switch from HTTPS to HTTPS. All of my domains are now working. My primary domain redirects from domain.com to blog.domain.com and switches from HTTP to HTTPS.

However, www.blog.domain.com redirects to blog.domain.com but does not switch to HTTPS.

This is the applicable code:

Code:
RewriteCond %{HTTPS} Off
# Rewrite HTTP to HTTPS
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R,L]
# Redirect to blog
RewriteRule ^(.*) https://blog.domain.com/$1 [NE,R=301,L]

What am I missing? Any help greatly appreciated.

Thank you.
 
Last edited:
Hey Merlin

Not sure if sorted but you could try too first redirect to your domain www with the certificate and then do the https redirect. Other thing to keep in mind is the amount of server requests. An example I came across:

Code:
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

[URL=https://mybroadband.co.za/vb/usertag.php?do=list&action=hash&hash=Now]#Now[/URL] , rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
do you have

RewriteEngine On

above in the .htaccess file?

Also, is mod_rewrite running? :D
 
Last edited:
Hi everyone,



I spent many, many hours struggling with this last night...

My host has just instituted Let's Encrypt, so I've edited all of my .htaccess files in order to switch from HTTPS to HTTPS. All of my domains are now working. My primary domain redirects from domain.com to blog.domain.com and switches from HTTP to HTTPS.

However, www.blog.domain.com redirects to blog.domain.com but does not switch to HTTPS.

This is the applicable code:

Code:
RewriteCond %{HTTPS} Off
# Rewrite HTTP to HTTPS
RewriteRule ^/(.*)$ https://%{HTTP_HOST}/$1 [R,L]
# Redirect to blog
RewriteRule ^(.*) https://blog.domain.com/$1 [NE,R=301,L]

What am I missing? Any help greatly appreciated.

Thank you.

https://github.com/ErikThiart/the-best-htaccess/blob/master/.htaccess
 
Hi everyone,



Thanks for your input.

static_sa, yup, it's all in place. This is just a subset of the file.

Thor, that's a nifty link.

Kasbah, your code definitely helped put me onto the right track. Much appreciated.

I've just gotten my code 100%. Here we go, in case it helps anyone else...

Primary...

Code:
# HTTPS
RewriteCond %{HTTPS} on
# No WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Redirect
RewriteRule ^(.*) https://blog.domain.com/$1 [NE,R=301,L]

Subdomains... (Hosted in public_html/anotherdummydomain/)

Code:
# HTTPS
RewriteCond %{HTTPS} on
# No WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Use URL
RewriteCond %{HTTP_HOST} !^(anotherdummydomain\.co\.za)?$
RewriteRule ^(.*) https://anotherdummydomain.co.za/$1 [NE,R=301,L]
 
UPDATE: Thor's link provides an even easier solution...

Code:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
 
You are welcome :love:

May+i+ask+good+sir+how+you+added+the+text+_9488a0829a3bb0b290882a4bf165ad97.jpg
 
Top
Sign up to the MyBroadband newsletter
X