Using htaccess to remove single query parameter

前端 未结 2 640
一向
一向 2021-01-14 23:24

For reasons that aren\'t worth going into here, Google have been indexing one of my sites with an unnecessary query string in the URL. I\'d like to modify my htaccess file t

2条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 23:38

    Try this (i tested it with your examples and it is working fine):

    RewriteEngine on
    
    RewriteCond %{QUERY_STRING} ^(.*)&?query=[^&]+&?(.*)$ [NC]
    RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L]
    

    Also, you should add a condition to apply this rule only on existing files (that's up to you).
    If you want it:

    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{QUERY_STRING} ^(.*)&?query=[^&]+&?(.*)$ [NC]
    RewriteRule ^/?(.*)$ /$1?%1%2 [R=301,L]
    

提交回复
热议问题