skip to Main Content

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


  1. 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.

    RewriteEngine ON
    RewriteCond %{THE_REQUEST} s/(.*.html)?p=d+s [NC]
    RewriteRule ^ /%1? [R=301,L]
    

    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.

    Login or Signup to reply.
  2. RewriteCond %{QUERY_STRING} ^p(&|$) [NC]
    RewriteRule ^ %{REQUEST_URI}? [R=301,L]
    

    This is almost correct, except the regex ^p(&|$) is incorrect. This matches p&<anything> or p exactly. Whereas you need to match p=<anything> (eg. ^p=) or p=<number> (eg. ^p=d+). This is of course assuming the p URL parameter always occurs at the start of the URL-path (as in your example).

    For example:

    RewriteCond %{QUERY_STRING} ^p= [NC]
    RewriteRule ^ %{REQUEST_URI}? [R=301,L]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search