Welcome to WebmasterWorld Guest from 50.16.112.199

Forum Moderators: phranque

301 doesn't redirect

want to force traffic from one domain to another

   
12:55 pm on Oct 11, 2011 (gmt 0)



I'm struggling with redirection. We own two domains: uglydomain.com and prettydomain.com. For the longest time our site was only available at uglydomain.com. Recently we purchased prettydomain.com and would like to migrate traffic over to the new domain. Setting it up so folks who browse to prettydomain.com get our content was easy. Where I'm having trouble is with the old url. I'd like anyone who still attempts to access our site at uglydomain.com to be automatically pushed to prettydomain.com. I attempted to do this via 301 in a .htaccess file but I think I'm doing something wrong. Here's what I did:


Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.uglydomain.com$[OR]
RewriteCond %{HTTP_HOST} ^uglydomain.com$
RewriteRule ^(.*)$ http://www.prettydomain.com/$1 [R=301,L]


When I browse to www.uglydomain.com I thought it would push me onto www.prettydomain.com (changing the url in my browser) but no such luck. Do I have the 301 right? What should happen? Thanks in advance for your assistance!
4:46 pm on Oct 11, 2011 (gmt 0)

5+ Year Member



Try this. Make sure you take backups.


Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^uglydomain\.com [OR]
RewriteCond %{HTTP_HOST} ^www\.uglydomain\.com
RewriteCond %{HTTP_HOST} uglydomain\.com
RewriteRule (.*) http://www.prettydomain.com/$1 [R=301,L]

[edited by: The_Fox at 4:48 pm (utc) on Oct 11, 2011]

4:47 pm on Oct 11, 2011 (gmt 0)

5+ Year Member



Can you also share the URl structure for the old and new domains? I assume they are the same.
4:54 pm on Oct 11, 2011 (gmt 0)



That did it. Thanks!
9:17 pm on Oct 11, 2011 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



Fox, can you explain this part?

RewriteCond %{HTTP_HOST} ^uglydomain\.com [OR] 
RewriteCond %{HTTP_HOST} ^www\.uglydomain\.com
RewriteCond %{HTTP_HOST} uglydomain\.com


It seems redundant, but obviously it isn't if the effect is different from the version without the third line.

btw, it should be possible to express the [OR] pair as

(www\.)?uglydomain\.com
9:45 pm on Oct 11, 2011 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



This is more robust.

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


Redirect if requested hostname is not "exactly" www.example.com

Make sure you escape any literal periods in all RegEx patterns.
12:54 am on Oct 13, 2011 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month




!^(www\.example\.com)?$

the '?' after the ')' makes the capture group optional, so hidden in g1smd's regexp is that it also matches when HTTP_HOST is exactly null.
this is a good thing as it handles the special case of HTTP/1.0 user agent requests.

HTTP_HOST for HTTP/1.0 browser:
http://www.webmasterworld.com/forum92/2240.htm [webmasterworld.com]
 

Featured Threads

My Threads

Hot Threads This Week

Hot Threads This Month