0

Say I wanted http://domain.com/product/?id=123 to become http://domain.com/product/foo, how do I do that in .htaccess? I tried something like this, but it didn't work:

RewriteBase /fisher
RewriteCond %{QUERY_STRING} ^id=123$    [NC]
RewriteRule ^/product$  /product/foo    [NC,L,R=301]

Suggestions?

NOTE: I do not need to capture the value of the id parameter as I will not use it in the new URL.

UPDATE 1:

RewriteEngine On
RewriteBase /fisher

RewriteCond %{QUERY_STRING} ^id=123 [NC]
RewriteRule ^product/$  /product/foo    [NC,L,R=301]
  1. Showing RewriteEngine and RewriteBase
  2. Remove $ in RewriteCond
  3. Remove / in RewriteRule

When I go to http://localhost/fisher/product/?id=123, nothing happens. The URL remains the same.

snoopy76
  • 103
  • 1
  • 3
  • Enable RewriteLog. This will give you more info on what is really happening here. – Krist van Besien Apr 09 '13 at 04:56
  • Do you want /fisher/product/?id=123 to be redirected, or /product/?di=123 ? your questions is a bit unclear here. Do you have your .htaccess in your webroot, or in a subdir of your webroot? – Krist van Besien Apr 09 '13 at 05:06

1 Answers1

0

I would suggest this. Similar to what you have, but with some subtle differences. Namely no $ after id=123 in the RewriteCond and adding a / after ^product in the RewriteRule:

  RewriteCond %{QUERY_STRING} ^id=123 [NC]
  RewriteRule ^product/$ /product/foo [NC,L,R=301]
Giacomo1968
  • 3,542
  • 27
  • 38
  • I see why you removed the `$`; it makes the regex less specific. I don't see why you added a `/` only; perhaps a `/?` (optional) might be better? In any case, it didn't work for me. My actual URL is this: http://localhost/fisher/product/?id=123. Does the `fisher` (my project name) affect anything? – snoopy76 Apr 08 '13 at 22:43
  • This should work, however I would not use R=301 while testing. 301 means "redirect permanent" which means that the next time your browser requests this resource it will do the redirect itself, without anything happening on the server. Use R=302. – Krist van Besien Apr 09 '13 at 05:14