Daryl Hansen
Daryl Hansen

Reputation: 13

htaccess RewriteCond only if specific parameters exist

I am using these 4 lines in my htaccess file to both redirect and rewrite a URL:

RewriteCond %{THE_REQUEST} /GetSearch1-test\.php [NC]
RewriteCond %{QUERY_STRING} ^Class=Residential\&Neighborhood\%5B\%5D=(.*)$ [NC]
RewriteRule ^ /Neighborhoods/%1\.html? [R=301,L]
RewriteRule ^Neighborhoods/([^.]*)\.html$ /GetSearch1-test.php?Class=Residential\&Neighborhood\%5B\%5D=$1 [NC,L]

It will take this URL: www.domain.com/GetSearch1-test.php?Class=Residential&Neighborhood%5B%5D=Wailea and redirect it to this URL: www.domain.com/Neighborhoods/Wailea.html

It works, but with major issues. Problem is, I only want it to do the redirect if only those two parameters exist (Class & Neighborhood). The first parameter is fixed, but the second one could be anything. If I add a third parameter to the URL, it still redirects and I don't want it to.

For example, if I go to this URL: www.domain.com/GetSearch1-test.php?Class=Residential&Neighborhood%5B%5D=Wailea&Parameter3=whatever

It still redirects. Any ideas would be greatly appreciated.

Upvotes: 1

Views: 1299

Answers (1)

Croises
Croises

Reputation: 18671

You can change 2nd RewriteCond with that:

RewriteCond %{QUERY_STRING} ^Class=Residential&Neighborhood\%5B\%5D=([^&]*)$ [NC]

Upvotes: 1

Related Questions