oshirowanen oshirowanen - 1 month ago 8
Apache Configuration Question

Permanent redirect via apache rewrite rules

I am trying to write a rule to permenantly redirect a domainname to another domain name

RewriteCond %{HTTP_HOST} ^www.companyname1.com$
RewriteRule ^(.*)$ http://www.companyname2.com/$1 [R=301,L]


This only works if the user remembers to type in www, if the user does not type in www in the url, the page will load but the image links will be broken.

Does anyone know how to adjust the above rule to it works with and without www?

I am using a LAMP configuration, apache 2 on redhat.

Answer

You can supply several optional Rewrite-Conditions with [OR]:

RewriteCond %{HTTP_HOST} ^www.companyname1.com$ [OR]
RewriteCond %{HTTP_HOST} ^companyname1.com$
RewriteRule ^(.*)$ http://www.companyname2.com/$1 [R=301,L]

This should do the trick. The first Rewrite-Condition fires, if www is present, the second one fires, if www has been forgotten.