views:

21

answers:

2

Hello,

I'd like to rewrite:

www.example.com/file.html?username=john&number=1234

To:

www.example.com/users/john

But I can't figure out how to extract the "username" value from the query string. I've been Googling this all morning and reading the official docs but no luck. I need to solve this problem with a rewrite, rather than changing the application.

Any help much appreciated! Rangi

A: 
RewriteCond %{QUERY_STRING}  username=([^&]+)
RewriteRule /?file.html /users/%1 

Going to http://example.com/file.html?username=foobar will then redirect you to http://example.com/users/foobar, add an [R] to the end if you need an external redirect.

Mostly the rewrites are done the other way around, it's rare to see someone who wants a querystring in 'outside' urls but doesn't have them internally. Or did I understand your question backwards?

Wrikken
Hi Wrikken, thanks for your reply. Yes I am actually trying to do it "backwards" compared to what most people are doing! Our old app used query strings but the new one uses cleaner URLs like /users/foobar, so I'm trying to redirect requests for the old app to the new app.
Rangi Robinson
A: 

Ok I've solved this using two rules, although not sure if I'm doing it the best way.

RewriteRule ^file.html xxx/%{QUERY_STRING} [L]
RewriteRule ^xxx/[^=]*=([^&]*) /users$1 [R=301,L]

The first rule makes the query string part of the URL, so the second rule can see it, and therefore match and rewrite parts of it. I used "xxx" but it could be anything.

Rangi Robinson