Peter Krauss
Peter Krauss

Reputation: 13930

Status 404 on valid rewrite, index handler expected

The location / have a @rewriteIt but something is worg...

Fresh installation of NGINX into UBUNTU 16 LTS, all apt standard.

NGINX is ignoring my rewrite, why? how to fix for example.com/foo redirect to test.php?

Testing status and dynamic pages, all fine except the rewrite:

With scripts

example.com

server {
        server_name example.com www.example.com;

        root /var/www/example.com/;
        index  index.php index.html index.htm;

        location / {   # ignoring here?
                try_files $uri $uri/ @rewriteIt =404;
        }

        location  @rewriteIt {  # something wrong here?
                rewrite ^/?([a-zA-Z0-9\-]+)/?$    test.php?obj=$1            last;
        }

        location ~ \.php$ {
          include snippets/fastcgi-php.conf;
          fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

Same when change to try_files $uri $uri/ @rewriteIt;.

default

server {
        listen   80; ## listen for ipv4; this line is default and implied
        listen   [::]:80 default_server ipv6only=on; ## listen for ipv6
        root /var/www/html;
        index index.php index.html index.htm index.nginx-debian.html;
        server_name localhost;
        location / {
                try_files $uri $uri/ =404;
        }

        location ~ \.php$ {
           include snippets/fastcgi-php.conf;
           fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        }
}

Upvotes: 1

Views: 38

Answers (1)

Richard Smith
Richard Smith

Reputation: 49692

The named location needs to be the last parameter of the try_files statement, replacing the =404 term. See this document for details.

There is also a second error. All URIs in nginx begin with a leading /, so the rewrite statement should specify /test.php?obj=$1 as the target.

Upvotes: 1

Related Questions