Htaccess redirects if url contains specific string

We installed new store software, and I set up redirection for all the old pages, but where I just can't work. If the url includes

cp_tpl = productlist.html

then I want to redirect to the main site. So, for example, the old URL might be

http://www.gocellular.ca/index.php?cp_tpl=productlist.html&cp_cat=803&cp_sid=1xxxxxx

and I want to redirect to

www.gocellular.ca

"cp_tpl = productlist.html" can be anywhere in the URL - basically I just want to check if the string "cp_tpl = productlist.html" is included anywhere in the URL and then redirected. I tried about 100 different .htaccess rewrites, but just can't get it to work! I would be very grateful for any ideas ... THANKS!

+3
source share
1 answer

To catch this string in the query string, as you showed it, you need to use RewriteCond, because the regular expression is not checked for the query string in the redirection rule. Something like this should do the trick:

RewriteCond %{QUERY_STRING} cp_tpl=productlist.html
RewriteRule .* / [R,L]

The above line will contain the query line. If yow wants to remove the query string, just add? after /, i.e.:

RewriteRule .* /? [R,L]
+4

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


All Articles