Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Re-direct two domains to one using rewrite



3:18 pm on Mar 24, 2005 (gmt 0)

10+ Year Member

Please bare with me as I'm new to all this and although I've done lots of reading lately it's taking some sinking in. I'm in the middle of moving hosts and although it may be a long winded way of doing it I set-up a new hosting account with a similar domain name so I could set everything up [MySQL, login scripts ect.] and test it before moving my old domain name over.

During testing I came across problems I'd never seen before,

The site has a members section and when logging in at 'www.newdomain.co.uk' the page refreshed to show the URL 'newdomain.co.uk' which caused script problems. After a lot of hair tugging I found the answer on this forum thank you.

I now have a .htaccess as below and it works just great.

RewriteEngine On
RewriteCond %{HTTP_HOST} ^newdomain\.co\.uk
RewriteRule (.*) [newdomain\.co\.uk...] [R=301,L]

Now when I move my old domain over to point at the same webspace as the new domain I envisage a similar problem.

What I would like to happen is which ever URL visitors come to [either www.newdomain.co.uk or www.olddomain.co.uk] I want them to be re-directed to www.olddomain.co.uk transparently if possible.

So would this work

RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.co\.uk
RewriteCond %{HTTP_HOST} ^newdomain\.co\.uk
RewriteCond %{HTTP_HOST} www\.newdomain\.co\.uk
RewriteRule (.*) [olddomain\.co\.uk...] [R=301,L]

One last thing should the dot in [olddomain...] be escaped as in Rewrite condition 3

Any help would be greatly appreciated thank you.



11:52 pm on Mar 24, 2005 (gmt 0)

10+ Year Member

1) You'll need to use the [OR] flag for RewriteCond to make that work, otherwise mod_rewrite will require *all* those rules to match in order to work (which, since you're checking three totally different host headers, will never happen. =)).

2) I'd stick a '$' at the end of each hostname in the RewriteCond, like this:

RewriteCond %{HTTP_HOST} ^www\.newdomain\.co\.uk$ [OR]

That's really optional (and me being pedantic. again), but it doesn't hurt to get into a good practice with these things. =)

3) make sure to prefix the hostname in your third RewriteCond with a ^

4) Literal '.' characters SHOULD be escaped in RewriteCond lines, since you're performing string matches. Literal '.' characters SHOULD be escaped on the *left* side (LHS) of RewriteRule lines. Literal '.' characters SHOULD NOT be escaped on the *right* side (RHS) of the RewriteRule lines, since that's simply a string which will be sent back to the browser (after variable expansion). Note that using them, in the incomplete test I ran, doesn't appear to hurt anything, but they're not needed*.

*I'd have to do some serious playing; it's *possible* that in certain exceptional circumstances escaping the literal '.' character would be required, but 99.99+% of the time (read: now), it's not. =)


2:11 am on Mar 25, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

2) I'd stick a '$' at the end of each hostname in the RewriteCond, like this:

RewriteCond %{HTTP_HOST} ^www\.newdomain\.co\.uk$ [OR]

I do not recommend end-anchoring hostname patterns. My reason is that it is valid to append a port number to the domain, and some browsers (such as Mozilla) will put that into the Host header. So, using the above example pattern, the server will see %{HTTP_HOST} as "www.newdomain.co.uk:80" and the RewriteCond will unexpectedly fail.

The other alternative is to use an end-anchor, but explicitly allow for appended port numbers, for example:

RewriteCond %{HTTP_HOST} ^www\.newdomain\.co\.uk(:[0-9]{1,5})?$ [OR]

or a variant of that.



12:34 pm on Mar 25, 2005 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

And Welcome to WebmasterWorld, Pollos. What a mouthful, huh? Gotta love responses loaded with excellent advice!


3:16 pm on Mar 25, 2005 (gmt 0)

10+ Year Member

jd: good point. That's one of those things I always forget about. =)


7:55 pm on Mar 25, 2005 (gmt 0)

10+ Year Member

Thank you all very much I'm sort of new to all this as my old hosting Co keeped evrything locked down the only code I could get to work was an error 404 redirect [not even a error 500 redirect worked] and everytime I asked support about htaccess I was told "this feature is not supported"

It's only now I can see how much control I can have over how the site works, looks like I've a lot of reading to catch up on.

Thanks again Steve.


Featured Threads

Hot Threads This Week

Hot Threads This Month