Rewrite before regex location

Francis Daly francis at
Wed May 4 22:00:18 UTC 2016

On Thu, May 05, 2016 at 02:56:27AM +0530, Joyce Babu wrote:

Hi there,

> When I add a
> regex location block to match .php extension, it gets higher precedence,
> and my rewrite rules are not applied.
> How can I resolve this?
> location /test/ {
>     rewrite "^/test/([a-z]+).php$" test.php?q=$1 last; }

Possibly using "location ^~ /test/" would work?

You may want to rewrite to /test.php (with the leading slash), though.

Although, better might be to just fastcgi_pass directly, rather than

Something like (untested)

  location ^~ /test/ {
    fastcgi_param SCRIPT_FILENAME $document_root/test.php;
    fastcgi_param QUERY_STRING q=$uri;
    include fastcgi_params;

although you would want to change the QUERY_STRING line to match what
you need; and you may need to switch the position of the "include",
depending on how your fastcgi server handles repeated params.

> location ~ [^/]\.php(/|$) {
>     fastcgi_split_path_info ^(.+?\.php)(/.*)$;
>     set $fastcgi_script_name_custom $fastcgi_script_name;
>     if (!-f $document_root$fastcgi_script_name) {
>         set $fastcgi_script_name_custom "/cms/index.php";
>     }

I suspect that it should be possible to do what you want to do there,
with a "try_files". But I do not know the details.

Good luck with it,

Francis Daly        francis at

More information about the nginx mailing list