Question

I moved my old store to magento, Im using 1.9.2 CE Magento. Old store was up and running for years and I didnt want to loose SEO URLs, so when I try to do 301 redirects, Im facing a problem.

If old URL has index.php in it, then instead of redirection to new URL, magento redirects to homepage.

I tried doing this with Magento default URL rewrites, with a third party extension and tried it with htaccess as well. Always the same result.

for example:

Following code in .htaccess

Redirect 301 /index.php?main_page=product_info&products_id=161 http://domain.com/catalogsearch/result/?q=sku-161 --- This redirects to homepage (Doesnt work properly)
Redirect 301 /main_page=product_info&products_id=331 http://domain.com/catalogsearch/result/?q=sku-331 --- This redirects to new URL (Works Properly) 

Problem is that, all my old URLs do have index.php? in it, so I need to make it work somehow.

Any ideas?

Thank You :)

Was it helpful?

Solution

It turns out the Redirect directive in an .htaccess file won't match against the query string, only the request uri (the part before the question mark). To match against the query string you'll need to use the RewriteCond and RewriteRule directives. More info on this here: https://simonecarletti.com/blog/2009/01/apache-query-string-redirects/

This should work in the .htaccess

RewriteEngine On

RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=331$
RewriteRule ^index\.php$ http://gearx.local/catalogsearch/result?q=sku-331 [R=301,L]

If all the urls you need to redirect are in the same format with a numeric id then these two lines should take care of them all:

RewriteCond %{QUERY_STRING} ^main_page=product_info&products_id=([0-9]+)$
RewriteRule ^index\.php$ http://gearx.local/catalogsearch/result?q=sku-%1 [R=301,L]

I'd recommend redirecting to the actual url of the product though rather than a search results page.

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top