I need to set 301 redirect in htaccess for URLs having parameter P. One example URL is
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html?p=1028
to
http://www.price4india.co.in/vivo-x20-plus-ud-price-in-india-scanner-feature-real.html
After redirect everything after .html shall get removed and the value after P=…… can be any numerical value. So far I have tried below query but it is not working. Any suggestion please…
RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]
2
Answers
With your shown samples, please try following .htaccess rules file. Make sure to keep your .html file and .htaccess files in root path only.
Make sure to clear your browser cache before testing your URLs.
NOTE: In case you have further more rules in your .htaccess rules, which includes internal rewrite of html files then you could keep these rules above those.
This is almost correct, except the regex
^p(&|$)
is incorrect. This matchesp&<anything>
orp
exactly. Whereas you need to matchp=<anything>
(eg.^p=
) orp=<number>
(eg.^p=d+
). This is of course assuming thep
URL parameter always occurs at the start of the URL-path (as in your example).For example: