b2238488
b2238488

Reputation: 1020

Get htaccess to add value as GET variable when redirecting

Simply put, what I want to achieve is this,

www.example.com/show-products/food should be redirected to www.example.com/show_products.php?cat_id=1

or www.example.com/show-products/clothes should be redirected to www.example.com/show_products.php?cat_id=8

But, I want htaccess to send the GET variables from the URL to the script as well. So www.example.com/show-products/food?sort=price&sort_order=asc should be redirected to www.example.com/show_products.php?cat_id=1?sort=price&sort_order=asc&cat_id=1

I know the way I formulated the question is not the best but hopefully you can make sense of it.

Thanks.


I found the answer, it's easier than I expected, the condition is:

RewriteRule ^food(/)?$ show_products.php?cat_id=1&%{QUERY_STRING}

Upvotes: 0

Views: 1390

Answers (1)

Tim Stone
Tim Stone

Reputation: 19169

The way that you've done it is fine, but keep in mind that you can also use the QSA flag. This automatically appends the existing query string to the query string which you generate with your rewrite, handling the concatenation for you automatically.

This would look like the following:

RewriteRule ^food(/)?$ show_products.php?cat_id=1 [QSA]

Upvotes: 4

Related Questions