homepage Welcome to WebmasterWorld Guest from
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

Redirecting .co.uk to .com
How to use a 301 redirect in .htaccess

 4:07 pm on May 14, 2003 (gmt 0)

Could someone explain to me how I can use a 301 Moved Permenantly redirect set up in my .htaccess file for the following situation...

I have two domains; www.domain.co.uk and www.domain.com

Both domains point to the same webspace, and I've only been marketing the .com version.

I need the www.domain.co.uk domain to convert to the .com version seamlessly.

In addition to this, some of the pages on my site are dynamic, so the solution needs to be able to retain query strings.

Thanks for your help in advance. :-)



 4:14 pm on May 14, 2003 (gmt 0)

Redirect Permanent /page [WebmasterWorld.com...]

You dont need the whole url for the page you are redirecting from.



 4:27 pm on May 14, 2003 (gmt 0)

Hi there,

So, I could do something like this...

RedirectMatch (.*)domain\.co\.uk$ [domain.com.*$...]



 4:34 pm on May 14, 2003 (gmt 0)

I donít know too much about regular expressions in the htaccess. There will be people here who will know for certain.

That said if you wish to forward all requests for one page (co.uk) to the other (com) you will need a htaccess file in the same place as the first which redirects to the second so the regex is not needed.


 4:48 pm on May 14, 2003 (gmt 0)

I wish to redirect traffic to *ANY* page which uses .co.uk to that same page using the .com (hence the regex).

I need to use a Mod Rewrite AFAIK, because the Redirect command loses POST and GET variables etc.

Thanks all the same.



 5:10 pm on May 14, 2003 (gmt 0)

I think I've found the solution! The following seems to work perfectly:

RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST}!^123\.456\.7\.89
rewriterule ^(.*)$ [domain.com...] [R=permanent,L]

Is this correct syntax? Can anyone varify this for me please?


 5:13 pm on May 14, 2003 (gmt 0)


Try something like this:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.yourdomain\.co.uk [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1?%1 [R=301,L]

The query string is passed through the backreference %1


 6:25 pm on May 14, 2003 (gmt 0)

Hi Jim,

A few questions, if I may...

1) My server doesn't like Options +FollowSymLinks, is that a problem?

2) What's the difference between your version and the one I posted?

My One:

RewriteCond %{HTTP_HOST}!^www\.domain\.com
RewriteCond %{HTTP_HOST}!^123\.456\.7\.89
rewriterule ^(.*)$ [domain.com...] [R=permanent,L]

Your One:

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


 6:43 pm on May 14, 2003 (gmt 0)


FollowSymLinks is required to enable mod_rewrite on some servers. If your redirects work without that directive, then you don't need to include it anyway.

There are several differences between the two rewrites posted here. The main difference is that I coded the second one to pass query strings - which you stated as a requirement. The first one won't pass query strings, unless there is something going on in your server that I don't know about.

A second difference is that the rewrite in the first post redirects requests to anything except the "preferred" domain and its IP address to the preferred domain. The second posted rewrite redirects only the .co.uk version of the domain (in either uppercase or lowercase letters) to the preferred .com domain.

Perhaps you didn't mean query string and don't use them:
In that case, either version should work, with the above-noted behavioral difference.

If you'd like to learn more about the subjects of mod_rewrite and regular expressions, try this useful tutorial and follow the links to even more info: Introduction to mod_rewrite [webmasterworld.com]



 6:50 pm on May 14, 2003 (gmt 0)

That's great Jim, thanks!

I've got your version up and it's working like a dream! ;-)

The php & cgi pages I have all use query strings (suprise!). Funnily enough, both versions seemed to work translating those, and the FollowSymLinks only resulted in a 500 error. Odd.

Anyhow, thanks for your help.



 1:49 pm on Jun 17, 2003 (gmt 0)

Hello again Jim,

Quick question...

I still have the following version in my .htaccess file:

RewriteCond %{HTTP_HOST} ^www\.domain\.co.uk [NC]
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ [domain.com...] [R=301]

It works correctly if I terminante a url with a /, however, if I leave this off, the server redirects to [domain.co.uk...]

My site is in my profile if you want to test it out.

Any ideas and/or help would be greatly appreciated. :-)


 2:00 pm on Jun 17, 2003 (gmt 0)

Actually, I think I've just cracked it...

The first RewriteCond should be:

RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co.uk [NC]

and that seems to solve any remaining issues.


 3:47 pm on Jun 17, 2003 (gmt 0)


Ah, good news! - Just got in and found it.



 3:56 pm on Jun 17, 2003 (gmt 0)

May be a bit OT here .. but I would just like to mention that there are some very good reasons from search perspective to do the otherway round .. ie keep .co.uk

We built a site 2 years ago on .com and realized too late that it won't show in google.co.uk , yahoo uk, aol uk etc.


 4:02 pm on Jun 17, 2003 (gmt 0)

There are diferences in regional engines, but we still get referals from google.co.uk etc.

Anyway, the site in my profile is predominantly for people based in countries other than the uk, so the .com is more appropriate.

Hope that helps. :-)

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