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.

I have the following location block, as part of a complex routing for a CMS:

    location @mylocation {

        if (-d $request_filename) {
            rewrite ^/(.*)$ /$controller/$siteName last;
            break;
        }

        if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
          rewrite ^/(.*)$ /$controller/$siteName/$1 last;
        }

    }

$controller is something like "index.php" and $siteName is a hash identifying the specific site in the cms.

It is working fine, nginx rewrites http://www.mydomain/path?somearg=something to http://www.mydomain/index.php/HASH/path?somearg=something.

But when I have an url like this http://www.mydomain/path/?somearg=something nginx redirects(301) to http://www.mydomain/index.php/HASH/path?somearg=something, and index.php and HASH are exposed.

I tried something like this:

    location @mylocation {

        if (-d $request_filename) {
            rewrite ^/(.*)$ /$controller/$siteName last;
            break;
        }

        if ($request_filename ~ "") {
            #not sure what to put here
        }


        if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
          rewrite ^/(.*)$ /$controller/$siteName/$1 last;
        }

    }

But I'm unsure what to put in the second if block in order to avoid the redirect.

Any help is appreciated, thanks.

share|improve this question
add comment

1 Answer 1

up vote 0 down vote accepted

Are you sure it's not the CMS generating the redirect? Nothing in your config or the way that nginx works would generate that kind of response.

Let's try dropping the trailing slash if one exists, and see if that placates the CMS from firing redirects?

if ($request_filename !~ "\.(js|htc|ico|gif|jpg|png|css)$") {
  rewrite ^/(.*?)/?$ /$controller/$siteName/$1 last;
}
share|improve this answer
    
Thanks works perfectly. –  broesch Feb 24 '12 at 12:03
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.