Webmasters that frequently work with Magento may have encountered the problems of Magento's URL Rewrite Management system. It can get quite messy, especially if you change a top-level category URL or store URL far after production.
For example, let's say you have a category that contains 20 sub-categories, each with an average of 20 products:
www.domain.com/category/subcategory-1 www.domain.com/category/subcategory-2 www.domain.com/category/subcategory-3 ...
Easy 301 Redirection with Magento's URL Rewrite Management
When fixing unique 404 errors, the URL Rewrite Management module does come quite handy.
- Start by choosing
Catalog > URL Rewrite Management > Add New URL Rewrite
- Choose Custom from the drop-down.
- Fill out the 4 provided fields to complete the 301 Redirect.
- ID Path: A unique ID for this rewrite so you can easily find it for future changes.
- Request Path: Type in the broken URL you intend to redirect.
- Target Path: The URL you wish to redirect the requested path to.
- Redirect: This should be set to Permanent (301).
- Done. Test if the request URL goes to the target URL correctly.
In some cases, the URL Rewrite Management method will be sufficient, but for those dealing with multiple URL's that need to go to the same target URL, this method may be less efficient.
Multiple 301 Redirects with .htaccess Regex
In a scenario where multiple URL subtrees need to be redirected, this method may prove most efficient. Make a backup of your .htaccess and consider looking into further references regarding .htaccess and its URL rewrites before attempting this method:
- Download and open your .htaccess file from the Magento root folder install.
- Place the rewrite rules (see some redirect examples below) after
RewriteEngine on, around line 117
- Refresh your Magento Cache after saving and uploading the updated HTACCESS file.
Here are some Regex Redirect Rules that may be useful.
Redirect Multiple URL's to OneExamples:
category/sub1 redirect to category/ category/sub2 redirect to category/ category/sub3 redirect to category/
To redirect all URL's that start with '/category/' to the same URL target, just a one-line Rule is needed:
RewriteRule ^category/ http://www.yourwebsite.com/category [R=301, L]
Redirecting Subcategory URL with New Parent URLExamples:
category/sub1 redirect to cat/sub1 category/sub2 redirect to cat/sub2 category/sub3 redirect to cat/sub3
This may be needed if you changed a parent Category URL or even if you want to remove the parent URL completely:
RewriteRule ^category/(.*) http://www.yourwebsite.com/cat/$1 [R=301, L]
OR to remove '/category/' completely from yourwebsite.com/category/sub and end up with yourwebsite.com/sub ('sub' can be any url):
RewriteRule ^category/(.*) http://www.yourwebsite.com/$1/ [R=301, L]
There are many more 301 Rewrite Rules to be aware of when fixing Magento 404 errors. Htaccess URL rewriting is independent of Magento, therefore consider a google search for specific rewrites.
When to use Custom URL Rewrites for Magento
I personally recommend to only consider using HTACCESS rewrites or even 'custom rewrites' when listed in Google Webmaster Tools.
Feel free to comment questions, fixes or other resources.