10

I am moving a Magento site from example.com/shopping to example.com and I have to 301 redirect all of the URLs to the new location (example.com). I am assuming I can use mod_rewrite in the .htaccess file to create a rewrite rule to do this? I tried to learn how to use mod_rewrite, but the syntax is so complex for me.

There are hundreds of pages that need to be redirected. Example:

http://www.example.com/shopping/product-category-1/product-sub-category.html

TO

http://www.example.com/product-category-1/product-sub-category.html

This is so that people don't end up with a 404 error when they enter the site via an old URL.

Jon Lin
  • 142,182
  • 29
  • 220
  • 220
peterbujok
  • 103
  • 1
  • 1
  • 4
  • 1
    I'm literally going to have to do the same thing in a couple hours and my Google query was going to be something like `.htaccess redirect single page`. It turns up good examples, like [.htaccess 301 redirect of single page](http://stackoverflow.com/q/1421068) – Pekka Sep 07 '13 at 01:02

1 Answers1

14

Try adding this to the htaccess file in your document root, preferably above any rules you may already have there:

RewriteEngine On
RewriteRule ^shopping/(.*)$ /$1 [L]

If you want to redirect the browser so that the new URL appears in the location bar, add an R flag in square brackets:

RewriteEngine On
RewriteRule ^shopping/(.*)$ /$1 [L,R=301]
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Ok, this is great advice, thank you. My next question would be: will this interfere with any other aspects of the default Magento .htaccess file? – peterbujok Sep 12 '13 at 15:15
  • @pbujok It will if magento expects there to be a `/shopping/` path in the URL, but it really depends on how you have it setup – Jon Lin Sep 12 '13 at 15:42
  • Will the [L] part prevent other rewrites from running that may be necessary for the functionality of Magento? Would it be better to just include R=301 and let the rest of the rewrites run? – peterbujok Sep 12 '13 at 16:10
  • @pbujok The rewrite engine loops, so after that rule gets applied, the new URI (will be one without `/shopping/` in it) will go through all the rules again. The `L` just stops rewriting in the current iteration. – Jon Lin Sep 12 '13 at 16:23
  • but to access admin, i can't use mysite.com/admin like earlier. Now i need to enter mysite.com/index.php/admin – Pankaj Upadhyay Jun 30 '15 at 15:51
  • For reference, in the case where `shopping` is a sub sub directory: `RewriteRule ^(.*)/shopping/(.*)$ $1/$2 [L,R=301]` – wkille Mar 26 '21 at 09:32