I've seen the trick on Apache's rewrite guide on how to redirect non-www requests to www requests:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.example.com/$1 [L,R]
Which redirects all requests for example.com to www.example.com.
But how do I add in that I want requests from example1.com, www.example1.com, example2.com, www.example2.com, etc. to redirect to www.example.com?
EDIT:
Here's the solution:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.example.com/$1 [L,R,NE]
Slightly different RewriteRule and added 'RewriteEngine on'
R
rewrite flag defaults to a 302 response code by default. If you want a 301 instead (which I think you do in most situations), you can do this instead:RewriteRule ^/?(.*) http://www.example.com/$1 [L,R=301,NE]
- Ian Dunnservice apache2 restart
- user1382306