php - Mod rewrite location header contains root file path
I'm trying to rewrite
https://example.com/vehicles.php?id=42
to https://example.com/vehicles/brand/honda
to handle my new URL change without affecting SEO. (i.e. "42" should be mapped to "honda")
I tried this.
RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^vehicles.php$ ^vehicles/brand/honda$ [L,R=301] #to handle old URLs to new URL mapping
RewriteRule ^vehicles/brand/honda$ ^vehicles.php?id=42$ [L] #to handle URL to file mapping
But it redirects to
https://example.com/root/site/%5evehicles/brand/honda$?id=42
which is incorrect in 2 ways.
- It includes a file path (
/root/site
) - I want the query param to be dropped.
I appreciate some help to get this fixed.
Answer
Solution:
Character ?
at the end of line removes query string from output path.
RewriteCond %{QUERY_STRING} ^id=42$
RewriteRule ^vehicles.php$ vehicles/brand/honda? [L,R=301]
Source