Search code examples
.htaccesshttp-redirectno-www

Redirect non-www to www without specifying domain name


I have a lot of websites using the same version of an htaccess file and I need to redirect all non-www to www using the same rule without specifying the domain name. The rule that does what I require is

 # Rewrite non www to www
 RewriteCond %{HTTP_HOST} ^example.com [NC]
 RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]

however I do not want the example.com's as it must work for all.

Thanks


Solution

  • You can use that:

    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]