Magento URL Rewriting, Regex and 301 Redirects Tips

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
...

URL Rewrite Tips from Matt Cutts
301 Redirect Tips by Matt Cutts
Now, if a change to the URL '/category' is made, Magento will automatically reindex all the sub-category URL's and those within them. On a development site, this can normally be avoided by unchecking the option to create redirects when changing URL's, but on a launched Magento site, you will need these 301 redirects to avoid 404 errors / missing pages. In addition, if you were to now make another change to the category URL or even just change it back to it's previous URL, it would create a whole 'new' rewrite, instead of removing the original rewrites, which does present some disadvantages.

Easy 301 Redirection with Magento's URL Rewrite Management

When fixing unique 404 errors, the URL Rewrite Management module does come quite handy.

    Magento 301 Redirect URLCustom URL Rewrite Fields
  1. Start by choosing Catalog > URL Rewrite Management > Add New URL Rewrite
  2. Choose Custom from the drop-down.
  3. 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).
  4. 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:

  1. Download and open your .htaccess file from the Magento root folder install.
  2. Place the rewrite rules (see some redirect examples below) after RewriteEngine on, around line 117
  3. 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 One

Examples:
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 URL

Examples:
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]

Resources

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.

I'm a developer at Maximus Business.
  • Justin
    Hey, nice article, it seems to be tough to find much useful on Magento's URL rewrite system. Couple of questions if you don't mind: in your experience, can ID path really just be any unique value, and it doesn't matter? My use case is: deleting a product, and wanting to redirect the old product page to a different existing product, something that seems reasonable enough to me, but I'm having a hard time figuring out how to do it correctly. My first attempt was to create a "product" rewrite rule to the new product page, which worked great until I refreshed the rewrite rules as a test and the target "pretty" url I had selected and want to be used as the canonical url was swapped out for the hideous system url. I did consider writing an apache rewrite rule as you suggested, but ideally I'd like the less technical store admins to be able to do this. My third attempt was to create a custom url rewrite as described here, and of course that survived the URL rewrite rule index flush because it's not associated with a product or category, but has the drawback on the other hand of not being associated with a product, if that target product URL were ever to change. Still, the whole arbitrary ID Path value seems so odd to me, so I figured I'd ask if you have any suggestions. I couldn't even find an extension to provide this functionality, something I'd happily pay for; sometimes I feel like I'm the first Magento user to care what their product/category URLs look like, and I just can't fathom it. Thoughts?

    Said on Oct 9, 2012 by Justin - Reply
  • Timon
    Hey, I found your blog doing a search. Essentially, i was wondering if I move a main category1under another main category2, will it automatically 301 redirect the old structure of site.com/category1 to site.com/category2/category1. Seems i found the answer that we have to it manually by hand :( Any extensions to automate this? I know when I change a product url (in the product settings) that it auto 301s old ur to new one. Shouldnt it be automatic for category changes. For a new store, I know i will change up the structure a lot to make browsing easier. This makes me fear changing the structure b/c when people link to certain products, then in the future they cannot find the exact product if the category structure has changed.

    Said on Apr 24, 2013 by Timon - Reply
  • Chetan
    Hi Maximilian, am trying to follow the steps and seem to be hitting a roadblock. I want to rewrite my product images to a CDN URL. Hence, http://www.yellowgiraffe.in/media/catalog/product/cache/1/small_image/222x/9df78eab33525d08d6e5fb8d27136e95/l/i/little-tikes-5-in-1-adjustable-gym-purple1_16.jpg should be picked up from http://cache.yellowgiraffe.in/1/small_image/222x/9df78eab33525d08d6e5fb8d27136e95/l/i/little-tikes-5-in-1-adjustable-gym-purple1_16.jpg Now I located an htaccess file in /var/www/media/.htaccess. Am reproducing the code from .htaccess below. I've tried this a couple of times, but the rewrite doesn't seem to be working. In addition I have clicked on all the buttons under "Cache Management". Grateful if you can help spot what's amiss. Options All -Indexes php_flag engine 0 AddHandler cgi-script .php .pl .py .jsp .asp .htm .shtml .sh .cgi Options -ExecCGI ############################################ ## enable rewrites Options +FollowSymLinks RewriteEngine on ##### Below Is An Experiment To Make CDN CSS work #RewriteRule ^media/css/(.*) http://css.yellowgiraffe.in/$1 [NC,R=301,L] RewriteRule ^media/catalog/product/cache/(.*) http://cache.yellowgiraffe.in/$1 [NC,R=301,L] ############################################ ## never rewrite for existing files RewriteCond %{REQUEST_FILENAME} !-f ############################################ ## rewrite everything else to index.php RewriteRule .* ../get.php [L]

    Said on Jul 24, 2013 by Chetan - Reply
  • Sameer Khan
    Glad to find this useful post. Got learned redirection methods in magento.... Thanks..

    Said on Oct 19, 2013 by Sameer Khan - Reply
  • Michael
    Thanks for this post, very useful! Just one note: I had to remove the space from '[R=301, L]' (so '[R=301,L]') to avoid getting an internal server error and the following msg in my logs: RewriteRule: bad flag delimiters.

    Said on May 20, 2014 by Michael - Reply
  • Donte
    Precisely what I was searching for, appreciate it for posting.

    Said on Sep 3, 2017 by Donte - Reply

Leave a Reply

Please feel free to give us your feedback and comment below. Please keep in mind that comments are moderated. Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>