.htaccess file redirecting http://www.domain.com to https://domain.com


Limp Gawd
Nov 9, 2004
So, I know virtually noting about the .htacess file other than some very basic ideas of what it is used for. Mine is huge (142 lines), I think primarily for the WordPress cache plugin I use, which makes it all that more confusing. But I digress.

My current site will redirect http://www.domain.com to https://domain.com and that seems to be a problem with my CDN as it wants everything to be on a www.domain.com.

So what I think I need is to change it from what it is doing now to redirecting to https://www.domain.com. I believe the lines in the file responsible for this are:

RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^domain.com

but I am unsure of how to modify them to achieve what I need. Those two lines appear in two different places in the .htaccess file. There is also these lines which may mean something:

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

and lastly, I may need to modify this line:

RewriteCond %{HTTP_HOST} ^domain.com

Which appears by itself (without the RewriteCond %{HTTPS} =on line above it).

Ideas? Suggestions? Pointers?
RewriteEngine On
# Redirect to the correct domain name
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^/?(.*)$ https://www.domain.com/$1 [NE,L,R=301]

Should do it. But realistically you should modify your Apache .conf instead of the .htaccess since that has to be parsed each hit.