views:

17

answers:

1

Using mod_rewrite, how would I take urls like these:

www.example.com/?foo=hello

www.example.com/?foo=world

and redirect them to urls like these:

www.example.com/hello/

www.example.com/world/

Thanks

+3  A: 

What you should be doing is the opposite of that. You make all URL links on your site look like the 'pretty' version (but don't change any other functionality), then you use mod_rewrite to make patterns that will match requests for a 'pretty' URL and show the appropriate 'ugly' url. A very simple example for your link...

RewriteEngine On
RewriteEngine ^([^/]+)/?$ /index.php?foo=$1

Update:

You can redirect in the opposite direction using...

RewriteEngine On
RewriteCond %{QUERY_STRING} ^foo=(.*)
RewriteRule ^index.php$ /%1/ [R=302]

Once you are happy it's working, you can change the 302 to 301 for a permanent redirect. If you combine this with the above example though you will get a 500 Internal Server Error since you will be creating a redirect loop.

Cags
Thanks for the reply.I understand what you're saying, but I'm trying to redirect a user that might have previously used an old ugly url to a new pretty one.
Micah
I updated my answer with information on how to forward in that direction.
Cags