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]
![Admin](/assets/logo_square_200-5d0d61d6853298bd2a4fe063103715b4daf2819fc21225efa21dfb93e61952ea.png)
Author by
Admin
Updated on August 01, 2022Comments
-
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 over 12 yearsThis might be a bit aggressive. Consider if the user has other domains they do not wish to redirect.
-
FrozenFire over 12 yearsThe RewriteCond of this rule set will limit the RewriteRule to only affect the mydomain.co.uk host.
-
Jason McCreary over 12 yearsNo, you are negating the condition (
!
). So this reads anything that's not mydomain.co.uk. -
FrozenFire over 12 yearsWhoops. You're right. My answer admittedly involved a copy+paste, and in my haste I missed that.
-
hawbsl about 8 yearsperfect! 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 about 8 yearsis it sufficient just to add a second RewriteCond after your first one ... like so:
-
hawbsl about 8 yearsRewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
-
anubhava about 8 years@hawbsl: Above condition makes
www
optional so it will serve bothwww.mydomain.co.uk
andmydomain.co.uk
domains in request. -
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.