301 redirect to new domain with same url structure

301 redirect to new domain with same url structure

August 21, 2026 · Illustrated author avatarBy Stefan Bogdanescu
301 redirect to new domain with same url structure

Redirecting a Magento Site to a New Domain While Preserving URL Structure

When migrating an e-commerce site like Magento to a new domain, ensuring that all pages redirect correctly is crucial for maintaining SEO value and user experience. In your case, you’re aiming to set up a 301 redirect from the old domain to the new one while keeping the same URL structure.

Firstly, let’s understand why your current .htaccess rule isn’t working as expected:

RewriteEngine On

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

This rule is designed to capture any incoming traffic and redirect it to the new domain’s home page (i.e., https://new-domain.com). However, it lacks specificity for capturing the path after the domain.

The Solution: Preserving URL Structure

To preserve the URL structure during redirection, you need a more complex rule that captures both the base domain and the path. Here’s an updated version:

RewriteEngine On

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

# If new domain is a subdomain of the old one (e.g., shop.old-domain.com to store.new-domain.com)
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com([:/\S]*)$ [NC]
RewriteRule ^(.*)$ https://new-domain.com%1 [R=301,L,NE]

This rule performs the following:

  • The first condition matches any subdomain or domain variants (with or without www) of the old domain.
  • The RewriteRule captures the entire path after the domain and redirects it to the new domain while preserving the same structure.
  • The second block is an optional rule for cases where the new domain is a subdirectory on the new server.

Why Is This Rule Better?

  1. Preserves URL Structure: It captures both the domain name and the path, redirecting https://old-domain.com/subdir/whatever to https://new-domain.com/subdir/whatever.

  2. Maintains SEO Value: A proper 301 redirect tells search engines that a page has permanently moved and should be crawled under the new URL.

  3. Improves User Experience: Users arriving at your old domain are seamlessly directed to the correct location on the new site without experiencing broken links or error messages.

Best Practices for Magento Site Migrations

When migrating a Magento site, consider these tips:

  • Always backup both the old and new sites before starting.
  • Test the redirection thoroughly using various URLs and scenarios.
  • Ensure that your SSL certificates are set up correctly on the new domain to avoid browser warnings.
  • Update your Google Search Console settings for the new domain.

Remember, migrating an e-commerce site is a complex process. If you find the technical aspects challenging, consider hiring a professional Magento development team from Laravel Company to ensure a smooth and secure transition.

Conclusion

Redirecting a Magento site while preserving URL structure requires careful configuration of your .htaccess file. By implementing the provided rule, you can ensure users and search engines are seamlessly redirected from your old domain to the new one without losing any SEO value or user traffic.

Remember, testing is key in migration processes. Thoroughly validate all page redirects before launching your new site. If you encounter any issues beyond what’s covered here, don’t hesitate to reach out to our team of experts at Laravel Company.

SEO

Our network