0

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

Alex
  • 1,060
  • 2
  • 17
  • 35

2 Answers2

2

You can use that:

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Croises
  • 18,570
  • 4
  • 30
  • 47
0

To add to the option list, it may be worth considering doing this at the sites level of Apache or NGinx. The benefit there, aside from having to maintain a bunch of .htaccess files, is that you could specify a 301 Moved Permanently header which many browsers will cache and redirect appropriately. Of course, if you are on a shared host or otherwise don't have access to the server, .htaccess may be your only option.

Kevin Eaton
  • 100
  • 3
  • 13