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

How can I rewrite this url api.domain.com/v1/json/account?id=123 to api.domain.com?id=123&v=v1&f=json&t=account

RewriteCond %{HTTP_HOST}        ^(api)\.[^\.]+\.[^\.]+$ [NC]
RewriteCond %{REQUEST_URI}      ^/([^/]+)/([^/]+)/([^/]+)/?(\?%{QUERY_STRING})?$ [NC]
RewriteRule ^(.*)$              /?%{QUERY_STRING}&v=%1&f=%2&t=%3 [L]
share|improve this question
    
1) Do you need to have that check for specific domain name (just asking); 2) Do you need to redirect ONLY this SPECIFIC URL (I mean, v1 always be v1, json always be json) or you need a bit more generic rule (where json may become xml); 3) Why QSA flag is not good (are you afraid that user may provide his own &v= value etc? 4) Where this rule will be placed -- in .htaccess or in config file (e.g. <VirtualHost> section)? 5) You want rewrite, not redirect? –  LazyOne Jun 30 '11 at 15:33
    
jep.. this rewrite only has to work for this specific subdomain.. and no, this is only an example.. I want a rewrite –  clarkk Jun 30 '11 at 18:14
    
As I understand you have resolved it yourself. Good :) –  LazyOne Jun 30 '11 at 23:23
add comment

2 Answers

RewriteCond %{HTTP_HOST}        ^api\.[^\.]+\.[^\.]+$ [NC]
RewriteCond %{REQUEST_URI}      /([^/]+)/([^/]+)/([^/]+) [NC]
RewriteRule ^(.*)$              /?%{QUERY_STRING}&v=%1&f=%2&t=%3 [L]
share|improve this answer
add comment

RewriteRule ^/([^/]+)/([^/]+)/([^/]+) /?v=$1&f=$2&t=$3 [QSA,R,L]

That should do it - you were very close. You are welcome to add your RewriteCond %{HTTP_HOST} one line above it (I have not tested that for you).

Let the Query-String-Append (QSA) flag do the hard work for you :). See the mod_rewrite documentation if you're curious about how that works.

Edit: Just to clarify, the only line you need to accomplish this is the one I have posted, it obviates your second RewriteCond and RewriteRule and collapses it into one statement. You may choose to add a single additional RewriteCond above it to check the value of %{HTTP_HOST}.

share|improve this answer
    
it doesn't work.. api.domain.com/v1/json/account/?id=123 is redirected to /v1/json/account/.. my rewrite looks like this: RewriteCond %{HTTP_HOST} ^(api)\.[^\.]+\.[^\.]+$ [NC] RewriteRule ^/([^/]+)/([^/]+)/([^/]+) /?v=$1&f=$2&t=$3 [QSA,L] –  clarkk Jun 30 '11 at 14:58
    
It definitely works, I tested it before I posted: /v1/json/account becomes /?v=v1&f=json&t=account and /v1/json/account/?id=123 becomes /?v=v1&f=json&t=account&id=123. Two things: 1) you removed the R flag from the rule (make it [QSA,R,L], you need it) and 2) just for testing purposes, remove the RewriteCond to ensure that is not your problem. –  loopforever Jun 30 '11 at 15:03
    
if you use the flag QSA, then the query string will override any of the v, f or t vars (if the query string contains any of them) –  clarkk Jun 30 '11 at 15:04
    
now the rewrite looks like this RewriteRule ^/([^/]+)/([^/]+)/([^/]+) /?v=$1&f=$2&t=$3 [QSA,R,L] but still the same... I'm requsting api.domain.com/v1/json/account/?id=123 –  clarkk Jun 30 '11 at 15:06
    
the rewritecond line is escaped –  clarkk Jun 30 '11 at 15:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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