Rick Helston
Rick Helston

Reputation: 763

Apache Redirect URL with and without WWW

How can I rewrite a URL with and without the www.? The following code in my .htaccess will rewrite domain1.com to domain2.com but not www.domain1.com.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain1.com
RewriteRule ^(.*)$ http://domain2.com/$1 [R=permanent,L]

Upvotes: 3

Views: 56

Answers (1)

Amit Verma
Amit Verma

Reputation: 41219

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain1.com$
RewriteRule ^(.*)$ http://domain2.com/$1 [R=permanent,L]

Upvotes: 3

Related Questions