Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to make my nginx redirects as clean as possible. I understand ~* is for case insensitive but I can only get it to work in example 2.

Example 1

rewrite ^/foobar redirect;

Example 2 -This works, but it isn't as efficient as the line above.

if ( $request_filename ~* foobar ) {
         rewrite ^ redirect;

Is there a way to do case insensitive redirects with example 1 without getting it too muddy?


share|improve this question
up vote 6 down vote accepted

I just had (and fixed) this same problem and ended up here trying to find the answer. The nginx documentation (, does not clearly state that the ~* only works inside an if statement, but apparently, that is the case.

To get case-insensitive regular expression matching for an ngnix URL rewrite outside of an if statement, I had to use the Apache/Perl style:

rewrite "(?i)foobar" redirect;

See (search for insensitive). It also seems that prefixing (?i) outside of a specific capture group makes it apply to the whole search string. Note: This does NOT seem to work with "^(?i)foobar" because it seems that the "^" is implied.

Just to be sure, though, and to make any future rewrites easier to maintain and less ambiguous if you end up doing a bunch of them, you may want to do something like this:

location /foobar {
     rewrite "(?i)" redirect;

Hope this helps...

share|improve this answer

What I've found to make this work:

rewrite ^/foobar redirect;

You only need to do this:

rewrite (?i)^/foobar redirect;

This just means prepend (?i) and otherwise everything is the same for matching.

share|improve this answer
This should be the accepted answer. – thekingoftruth Sep 5 '14 at 8:55

Your Answer


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.