htaccess – How to remove a single query parameter from a URL

The following htaccess code rule shows how to remove a single query parameter from a URL – leaving any existing parameters.

This is done using a rewrite rule and will do a 301 redirect to the new URL path.

This this example we’ll be removing the query parameter “query_var” – regardless of it’s place in the existing URL query string.

Original URLNew URL
http://example.com/?query_var=12345http://example.com/
http://example.com/?var1=keyword1&query_var=12345http://example.com/?var1=keyword1
http://example.com/?query_var=123455&var1=keyword1http://example.com/?var1=keyword1

 

RewriteEngine on

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

 

See it in action … 

 

Reference: https://stackoverflow.com/questions/21857280/using-htaccess-to-remove-single-query-parameter

Was this article helpful?

Related Articles