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.

No matter what tools I try, it never works quite right.

I have an api, that is called at /api/v1/method.format?arg1=x&arg2=x

I have this .htaccess on /api/v1/:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([^.]+).([^?]+)$ api.php?method=$1&format=$2&%1

This is what gave me better results, but didn't accept the additional arguments:

location /api/v1.1/ {
        if (!-e $request_filename) {
          rewrite ^/([^.]+).([^?]+)$ /api/v1.1/api.php?method=$1&format=$2&%1;
        }
    }

Any ideas? I tried many tools online but none of them gave me an acceptable outcome :|

Thanks.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

There are several problems with your regex.

  1. nginx tries to match the regex against the full request URI, that is including /api/v1.1/.
  2. Additional args are not included, because you force the string to end with $ sign, before any ? can appear.
  3. You should not use the inverse character classes [^.] and [^?], as they will allow far more characters then you want. The first one accepts any character aside from dot, that means slashes could also be part of your method name.
  4. The . symbol in a regex matches any character, but you wanted only the literal dot, you have to escape it with a slash \.
  5. You should append the break flag at the end of the rewrite statement, so that no redirects are sent to the client and the rewrite is processed internally by nginx.

So here's my suggestion:

location /api/v1.1/ {
    if (!-e $request_filename) {
      rewrite ^/api/v1\.1/([a-zA-Z0-9]+)\.([a-zA-Z0-9]+)?(.*) /api/v1.1/api.php?method=$1&format=$2 break;
    }
}
share|improve this answer
    
That did the trick. Thanks! –  MegaEduX May 6 '13 at 1:41

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.