Redirecting Magento Site After Moving to Subdirectory
Moving an e-commerce site from the root directory to a subfolder is a common operation during migrations or when transitioning to a new platform. However, it’s crucial to ensure that all old URLs are correctly redirected to the new location to maintain SEO value and user experience.
Understanding the Problem: Incorrect Redirect in Root .htaccess
The provided htaccess code attempts to redirect all requests from the root folder to the /shop subfolder. The first rule uses a negative lookahead assertion (?!), which typically works for preventing loops, but it seems unnecessary here since you’re not rewriting any paths inside the loop.
The Issue with Redirecting to Root URL
When you redirect all requests to http://www.example.com/shop$1 [R=301,L], it’s telling the server to send a 301 permanent redirect status code for every request. However, instead of appending the original path after /shop/, it’s simply sending a redirect to the root URL.
Solving the Redirect Issue: Proper Rewrite Rules in Subdirectory
The solution lies not in modifying the root .htaccess file but rather in updating the rewrite rules within the shop subfolderâs .htaccess (typically located inside your Magento installation).
After moving your site, you need to include a RewriteBase directive that specifies the document root for your Magento instance. This tells Apache where the base of your application lives.
Correct Rewrite Rules for Magento 2
For Magento 2, ensure you have the following rewrite rules in your subfolderâs .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /shop/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /shop/index.php [L]
</IfModule>
Key points to note:
RewriteEngine On: Enables the rewrite module.RewriteBase /shop/: Sets the document root of your Magento instance to/shop/.- The remaining rules handle rewriting requests that don’t correspond to an existing file or directory, ensuring they all route through
index.php.
Conclusion and Best Practices
Always remember to update your rewrite rules after moving your site to a subdirectory. This ensures that old URLs are correctly redirected to the new location, preserving SEO value and providing seamless user navigation.
For more detailed guidance on Magento 2 SEO best practices and advanced .htaccess configurations, I recommend checking out our comprehensive guide at Laravel Company.
By correctly implementing these rewrite rules in your subdirectory’s .htaccess, you can avoid the pitfall of redirecting all pages to a root URL and maintain the integrity of your siteâs URL structure during migrations.