简体   繁体   中英

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?

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.

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM