Forum Moderators: phranque

Message Too Old, No Replies

Can Mod Rewrite redirect WWW and non-WWW requests?

... this only works for non-www requests.

         

Eric4

6:25 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



Hello, (first post:-)

I've parked old_domain.com => new_domain.com, and also need the old_domain to redirect to new_domain.com in the browser window.

What I put in .htaccess (below) works great for non-"www" requests to old-domain.com, but does not redirect www.old_domain.com in the browser window to www.new_domain.com.

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^old_domain\.com
RewriteRule ^(.*)$ [new_domain.com...] [R=301,L]

I've tried to add the www to RewriteCond, such as ^www\.old_domain\.com , but it still won't redirect both www and non-www requests.

Is there a way to redirect both WWW and non-WWW requests using mod rewrite?

Thanks,
Eric

Span

6:35 pm on Apr 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to the forums, Eric.

A small change in your RewriteCond will do what you want. I added a NoCase flag for users that like to shout..

RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com [NC]

jd01

6:37 pm on Apr 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Eric,

You could make the www in the condition optional:

RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com

or, if the only requests that your .htaccess file will 'catch' are for the old domain, you could remove the condition entirely:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http//www.new_domain.com/$1 [R=301,L]

'?' matches the preceding character or group of characters 0 or 1 time.

Hope this helps.

Justin

Eric4

6:59 pm on Apr 27, 2005 (gmt 0)

10+ Year Member



Span & Justin,

Thank you for the quick reply .. works like a charm!

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?old_domain\.com [NC]
RewriteRule ^(.*)$ [new_domain.com...] [R=301,L]

Kind regards,
Eric

<snip>

[edited by: jdMorgan at 8:12 pm (utc) on April 27, 2005]
[edit reason] Removed tag line per TOS. [/edit]