Web Hosting Talk







View Full Version : mod_rewrite redirect without query string parameters


fitz
02-21-2007, 03:10 PM
Hi,

I am trying to use mod_rewrite to redirect some old URLs to new ones. I need to redirect a URL like 'services.php?id=2' to simply '/services/all' with no query string parameters. Using the following adds the ?id=2 to the end of the URL but I want a fresh redirect without this:
RewriteCond $1 ^services.php(.*)
RewriteRule ^(.*) /services/all [L,R=301]

Can anyone help?

Thanks a lot!

LinuXice
02-22-2007, 05:38 PM
Have tried using this?

RewriteCond %{QUERY_STRING} ^id=([0-9]+)
RewriteRule ^services\.php$ /location/to/services/all/index.html [R=301,L]

fitz
02-22-2007, 06:22 PM
Thanks for your reply. I don't have an index.html page so that wouldn't work (its forwarded again via rewrite to a PHP script). Turned out all I needed was a '?' at the end of the rewrite to override the query string:

RewriteCond $1 ^services.php(.*)
RewriteRule ^(.*) /services/all? [L,R=301]

I guess as you've pointed out a neater solution would be:

RewriteRule ^services\.php$ /services/all? [L,R=301]

LinuXice
02-22-2007, 08:01 PM
Np, the index.html was just an example. :)