0

I'm assuming this is an easy one for anyone with nginx knowledge.

I'm calling a URL like this:

/site-preview/css/custom-styles.css?org=myorg

The file custom-styles.css doesn't actually exist. So, I want to rewrite the URL to actually serve this:

/css/custom-styles.php?org=myorg

I'm coming from the apache world where I have this working in my .htaccess file.

In my nginx config I've tried things like:

location /site-preview {
    rewrite ^/site-preview/css/custom-styles.css?(.*) /css/custom-styles.php?$1 last;
}

and:

location /site-preview {
    rewrite ^css/custom-styles.css?(.*) /css/custom-styles.php?$1 last;
}

as well as having the rewrite outside of a location block.

Thanks in advance.

tptcat
  • 247
  • 1
  • 2
  • 7
  • Your rewrite rules are for the extension `.cs` and `.css` followed by anything or nothing; the second example won't match anything since the path always starts with `/`. Probably a good idea to look at [the docs for $args, $is_args etc.](http://nginx.org/en/docs/http/ngx_http_core_module.html#variables) - get args are not in the string the rewrite directive processes. – AD7six Dec 01 '14 at 17:36

1 Answers1

2

The nginx documentation for rewrite (http://nginx.org/en/docs/http/ngx_http_rewrite_module.html) states that unless you end the replacement string with a '?' all previous qstring vars are appended. This means you don't need to (and probably shouldn't be) adding your qstring vars to the replacement.

Also I would be as specific as I could be on the location (adding ../css/.. path:

location /site-preview/css {
  # this is a blind rewrite passing qstr along
  rewrite custom-styles.css /css/custom-styles.php last;

  # this one does NOT pass along qstr (using the trailing '?')
  rewrite custom-styles.css /css/custom-styles.php? last;
}
  • Thank you. I eventually figured that bit out about the query string and ended up with something very similar to what you have. – tptcat Dec 01 '14 at 19:24