1

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?

3

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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