Search code examples
apache.htaccessmod-rewrite

redirecting https://domain.com to https://www.domain.com


I've recently moved my site to HTTPS and im trying to force all http traffic to https and also to the www. version

i.e forcing https://domain.com to https://www.domain.com

I have this in my htaccess and all http to https works, apart from the redirect from https://domain.com to https://www.domain.com

What am i doing wrong?

RewriteEngine  on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

Solution

  • You need an OR condition here:

    RewriteEngine  on
    
    RewriteCond %{HTTPS} off [OR]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.domain.com%{REQUEST_URI} [R=301,L,NE]