Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

301 redirect to new domain with same url structure

I have a Magento website that is moving to a new domain. Im looking to 301 redirect all pages from the old domain to the new domain keeping same url structure.

I've updated the .htaccess file on my old domain with:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^new-domain.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]

The issue: The above seems to redirect every instance of: old-domain.com/subdir/whatever only just to the main domain: new-domain.com.

I'd be looking to redirect old-domain.com/subdir/whatever to: new-domain.com/subdir/whatever.

Any idea on what could be wrong?

like image 510
user1231561 Avatar asked Mar 06 '15 08:03

user1231561


2 Answers

Adjust your rule to use REQUEST_URI variable which is not dependent on the directory of .htaccess:

RewriteEngine On

RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://new-domain.com%{REQUEST_URI} [R=301,L,NE]

Better to clear your browser cache before testing this rule.

PS: You need to match hostname condition to old-host not the new-host.

like image 56
anubhava Avatar answered Sep 23 '22 23:09

anubhava


This should redirect and retain the path after the main domain.

RewriteEngine on
RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC]
RewriteRule ^(.*)$ http://new-domain.com/$1 [R=301,L]
like image 28
Adam Paterson Avatar answered Sep 25 '22 23:09

Adam Paterson