enchance
enchance

Reputation: 30501

htaccess: remove selected key=value pair from query string

I've been trying to work my head around this but my .htaccess powers just aren't as good as I thought. I have this sample query string:

http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana

Is it possible to do this using .htaccess:

  1. Check if type=fresh exists. If not then redirect to page index.php
  2. If type=fresh exists, remove it but retain the rest of the query string

Upvotes: 0

Views: 840

Answers (1)

Jon Lin
Jon Lin

Reputation: 143926

You can use mod_rewrite to match against the query string. In your .htaccess:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)type=fresh&?(.*)$
RewriteRule ^(.*)$ /$1?%1%2   [L,R]

This will make it so if someone enters http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana in the browser, their browser will get redirected to http://mycoolsite.com/store.php?a=apples&b=banana . If you want the redirect to happen internally (so the browser's location bar doesn't change), remove the ,R in the brackets at the end of the RewriteRule.

Upvotes: 1

Related Questions