homepage Welcome to WebmasterWorld Guest from 23.22.217.122
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Re-direct two domains to one using rewrite
Pollos




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

Hi
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.

Steve

 

sitz




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

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. =)

jdMorgan




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

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.

Jim

coopster




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

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

sitz




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

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

Pollos




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

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved