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.

My goal is that requests for /foobar?key=123 should redirect to /download/foobar.html?key=123.

I currently use the following code to accomplish this:

location /foobar {
  rewrite ^/foobar(.*)$ /download/foobar.html$1 permanent;
}

This does the trick, but we have some old code that also calls /FooBar?key=123 and /Foobar?key=123 which ignores the rewrite rule.

How can I get these to rewrite too? Could use:

location ~* ^/foobar {
  rewrite ^ /download/foobar.html$1 permanent;
}

but isn't this a bit loose of a rule?

share|improve this question

1 Answer 1

up vote 4 down vote accepted

First off, location is case sensitive by default; location ~* ^/foobar$ will fix this.

For the rewrite, it's always case sensitive, but you can make your expression match both forms:

rewrite ^/[Ff]oo[Bb]ar(.*)$ /download/foobar.html$1 permanent;
share|improve this answer
    
I think without the ^/foobar it could erroneously match somewhere else, like /images/foobar12.png. Or does the rewrite ^/[Ff]... take care of that? –  jimg Sep 19 '11 at 21:55
    
@jimg Indeed it will. And without a trailing $, it'll also catch /foobars, so we'll add that, too (location doesn't look at the query params, so this will work just fine in your case). Yes, the string limiting in the rewrite takes care of that - but if all you're doing in that location block is the rewrite, then you can probably just get rid of the location completely. –  Shane Madden Sep 19 '11 at 22:02
    
Thanks for the update Shane. so you would suggest just placing it in the location / block to simplify? Isn't it better to split off rewrites into their own block for performance reasons? –  jimg Sep 19 '11 at 22:15
    
@jimg Since there'd be a regex match needed for the location anyway, the performance impact to evaluating the rewrite for every request is a minimal increase. –  Shane Madden Sep 19 '11 at 22:24

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.