NoNice
NoNice

Reputation: 411

htaccess redirect with variable query params

could you help me get this working please.

I am trying to redirect

/search.php?id=5GHU&distance=50&sort=title

or

/search.php?id=5GHU

or

/search.php?id=5GHU&distance=50

to

/search/?query_string_values

i.e. whether it's one query string parameter or many they all should go there

This is what I tried.

RewriteBase /

RewriteCond %{QUERY_STRING} (?:^|&)id=([^&]+)
RewriteRule ^search\.php$ /search/%1? [L,R=permanent]

Thanks

Upvotes: 0

Views: 57

Answers (1)

Nic3500
Nic3500

Reputation: 8611

Sorry for my confusion about your question, I got it now. So...

Modify your .htaccess like this:

RewriteEngine On
RewriteRule "^/search.php$"    "/search/"    [L,R=301,QSA]

In short it will take the query string (what follows /search.php that starts with ?) and append it to "/search/". That is done by the QSA flag to RewriteRule. No matter how many parameters you have, it will append it all.

See https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa

So

/search.php?a=1&b=2  -->    /search/?a=1&b=2
/search.php?c=3      -->    /search/?c=3

Since /search/?a=1&b=2 does not specify a page to use, it will use the default page defined by DirectoryIndex in your configuration. I prefer to explicitly specify the page, but it works without.

Upvotes: 1

Related Questions