Using htaccess to remove single query parameter

前端 未结 2 638
一向
一向 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]
    
    0 讨论(0)
  • It seems there is a mistake or something. Here is working solution:

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

    This code removes just one parameter. Working perfect.

    0 讨论(0)
提交回复
热议问题