Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I've got a rewrite like this:

RewriteRule /products.php?id=123&t=12345 /product/New-Product-Name

If I turn on RewriteLogging, and turn the level up, I get the following:

applying pattern '/products.php?id=123&t=12345' to uri '/products.php'

Why is the full uri (including querystring) not being looked at?

share|improve this question

1 Answer 1

up vote 1 down vote accepted

From the mod_rewrite docs:

Note: Query String

The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.

In short, you'll need to use a combination of RewriteCond and RewriteRule; maybe something like:

RewriteCond %{QUERY_STRING} ^id=123&t=12345$
RewriteRule /products.php /product/New-Product-Name

Update: I tested this, and it works as I described. It redirects to
/product/New-Product-Name?id=123&t=12345.

If you want the query string ?id=123&t=12345 to be removed, add a ? to the RewriteRule, like:

RewriteRule /products.php /product/New-Product-Name?

which will redirect to
/product/New-Product-Name

I'm imagining there's a better way to do this using RewriteMap, but this might do for a one-off.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.