views:

37

answers:

2

Using mod_rewrite, how could I turn URLs that follow this pattern:

http://example.com/index.php?id=14

Into this pattern:

http://example.com/14/

+1  A: 
RewriteCond %{QUERY_STRING} id=(.+)
RewriteRule ^/index.php$ /%1/? [R]
Ignacio Vazquez-Abrams
+1  A: 

As I think that you rather want to rewrite requests to /<ID>/ internally to /index.php?id=<ID>, try this:

RewriteRule ^(\d+)/$ index.php?id=$1

But if you really want to redirect requests to /index.php?id=<ID> internally to /<ID>/:

RewriteCond %{QUERY_STRING} (^|&)id=(\d+)(&|$)
RewriteRule ^index\.php$ /%2/? [R]

And if you want to preserve possible other URL arguments:

RewriteCond %{QUERY_STRING} ^(([^&]*&+)*)id=(\d+)(&+(.*))?$
RewriteRule ^index\.php$ /%3/?%2%5 [R]
Gumbo