htaccess redirect .co.uk to .com for all pages

12,595

Solution 1

RewriteEngine On
RewriteBase /

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

This redirects both the www and non-www for mydomain.co.uk to www.mydomain.com.

Solution 2

To redirect any (sub-)domain other than mydomain.com, use

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]

Might be useful if you have other domains pointing to the same website.

It also redirects the www.mydomain.com to mydomain.com.

Solution 3

This is accomplished using a simple rewrite placed in your .htaccess file.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.co.uk$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [L,R=301]
Share:
12,595
Admin
Author by

Admin

Updated on August 01, 2022

Comments

  • Admin
    Admin almost 2 years

    Im migrating my website from the .co.uk to the .com but need to setup a 301 redirect so all of the individual pages will still be routed properly.

    ie i want http://www.mydomain.co.uk/shopping/product1 to go to http://www.mydomain.com/shopping/product1

    I have done this before but for the life of me cannot remember how.

    many thanks

    paul

  • Jason McCreary
    Jason McCreary over 12 years
    This might be a bit aggressive. Consider if the user has other domains they do not wish to redirect.
  • FrozenFire
    FrozenFire over 12 years
    The RewriteCond of this rule set will limit the RewriteRule to only affect the mydomain.co.uk host.
  • Jason McCreary
    Jason McCreary over 12 years
    No, you are negating the condition (!). So this reads anything that's not mydomain.co.uk.
  • FrozenFire
    FrozenFire over 12 years
    Whoops. You're right. My answer admittedly involved a copy+paste, and in my haste I missed that.
  • hawbsl
    hawbsl about 8 years
    perfect! how to take care of www and non-www for the .com as well? so that all four scenarios (www & non-www, .co.uk and .com) all go to the same www.mydomain.com?
  • hawbsl
    hawbsl about 8 years
    is it sufficient just to add a second RewriteCond after your first one ... like so:
  • hawbsl
    hawbsl about 8 years
    RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
  • anubhava
    anubhava about 8 years
    @hawbsl: Above condition makes www optional so it will serve both www.mydomain.co.uk and mydomain.co.uk domains in request.
  • Kay
    Kay about 8 years
    @hawbsl Not sure if it's relevant in your case, but since the original question was apparently related to a webshop (judging by the URLs), you might still want to specify if your question is about regular ole HTTP or also involves secure connections (HTTPS) at some point.