Htaccess redirect with portable variable

I am trying to redirect a large number of 404s. Some old sites related http://example.com/product.tmpl?SKU=XXX
to XXXis a SKU number.

I want it redirected to
http://example.com/product.php?SKU=XXX

I got here, but it still doesn't work. Do I have a typo in my code?

RewriteEngine On
RewriteCond %{QUERY_STRING} ^SKU=([^&]+)
RewriteRule ^/?product\.tmpl$ /product.php?SKU=%1 [L,R=301]

EDIT: my full htaccess file reads like this:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^SKU=([^&]+)
RewriteRule ^/?product\.tmpl$ /product.php?SKU=%1 [L,R=301]

# BEGIN WordPress

<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress
+4
source share
1 answer

Just try:

RewriteRule ^/?product\.tmpl$ /product.php [L,QSA]

The QSA modifier saves the query string as it is: http://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

0
source

Source: https://habr.com/ru/post/1608388/


All Articles