1

so this one is a weird one! I have these nginx stanzas. I used an online tool to convert .htaccess rules into nginx capable ones.

location / {
        rewrite ^/(([^/]+/)*)login/$ /login.php?path=$1&$query_string break;
        rewrite ^/(([^/]+/)*)comment/$ /comment.php?path=$1&$query_string break;
        rewrite "^/([^/]+/){4}$" /asset.php break;
        rewrite ^/(\d\w+)/$ /public.php?ticket=$1 break;
        rewrite "^/(\d\w+)/([^/]+/){2}?$" /public-asset.php?ticket=$1 break;
        if (!-e $request_filename){
                rewrite ^/(([^/]+/)*) /index.php?path=$1&$query_string break;
        }
        try_files $uri $uri/ /index.php$is_args$args;
}

location ~ \.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_pass   unix:/var/run/php/vh1-fpm.sock;
        include fastcgi_params;
}

going to https://example.test/ ok

going to https://example.test/login/ all heck break loose and PHP code is flooded onto the screen.

The regex routing works because the redirects take place. but once the redirect it done, it's like the masked .php file is not caught. All this is fixed if I encapsulate the PHP checks within the / location block.

1 Answer 1

1

You cannot use rewrite ... break when the rewritten URI is to be processed in a different location block.

Use: rewrite...last


Also, there is no need to tack &$query_string on the end of the rewritten URI, as rewrite will do it anyway unless the rewritten URI ends with a ?.

See this document for details.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.