The following is a very simple and straightforward method to force, in Apache, non-WWW domains to WWW. There is also an example of how it would work with HTTPS. These can be used in httpd.conf or in a .htaccess file.
|
1 2 3 |
RewriteEngine on RewriteCond %{HTTP_HOST} !^www\. RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] |
To maintain HTTPS:
|
1 2 3 |
RewriteCond %{HTTP_HOST} !^www\. RewriteCond %{HTTPS}s ^on(s)| RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L] |
By the way, the reverse (WWW to non-WWW) is as simple as this:
|
1 2 3 |
RewriteEngine On RewriteCond %{HTTP_HOST} ^www\. RewriteRule ^ http://yourdomain.com%{REQUEST_URI} [R=301,L] |
Unfortunately, it’s not as simple, though I’m sure someone can provide a better example.
