.htaccess redirect some Opencart pages to the new domain

52 Views Asked by At

I am having trouble setting up redirects from the old website pages to the new domain.

Opencart Version 1.5.3.1

I need to set up redirects for 5-10 pages of a similar format. For example:

/index.php?route=product/product&path=2_8&product_id=NNN

I would appreciate any assistance.

My .htaccess right now:

RewriteBase /
RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

A redirect rule of the following kind is not working:

Redirect 301 /index.php?route=product/product&path=2_8&product_id=135 https://newdomain.com/new_page/
1

There are 1 best solutions below

2
MrWhite On

My .htaccess right now

I assume you also have a RewriteEngine On directive at the top of the file? (Otherwise, you need it!)

Redirect 301 /index.php?route=product/product&path=2_8&product_id=135 https://newdomain.com/new_page/

As mentioned in comments, the Redirect (mod_alias) directive matches against the URL-path only, so the above directive (that includes a query string) will never match.

Since you are already using mod_rewrite, you should use mod_rewrite for this redirect as well and you need to combine this with a RewriteCond directive in order to match the query string.

For example, your rule should be like this at the top of the .htaccess file, before your existing directives:

RewriteCond %{QUERY_STRING} =route=product/product&path=2_8&product_id=135
RewriteRule ^index\.php$ https://newdomain.com/new_page/ [QSD,R=301,L]

Note that the = prefix on the preceding CondPattern makes this an exact string match, not a regex.

The QSD flag (Apache 2.4) discards the original query string from the redirected request.

UPDATE:

When using QSD in RewriteRule, I was getting a 500 error.

That would suggest you are on an (outdated) Apache 2.2 server. In that case you would need to remove the query string by appending an empty query string to the substitution string. (The browser then strips the trailing ? from the request.)

For example:

:
RewriteRule ^index\.php$ https://newdomain.com/new_page/? [R=301,L]