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

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.

share|improve this question

marked as duplicate by Jacob, mdpc, EEAA, Andrew, Ward Apr 9 '13 at 5:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
Enable RewriteLog. This will give you more info on what is really happening here. –  Krist van Besien Apr 9 '13 at 4: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 9 '13 at 5:06

1 Answer 1

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]
share|improve this answer
    
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: localhost/fisher/product/?id=123. Does the fisher (my project name) affect anything? –  snoopy76 Apr 8 '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 9 '13 at 5:14

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