In the root directory, any redirect to localhost/ is simply redirected to index.php, but if you go inside the localhost/phone subdirectory, there's an index.php page too, and as you type localhost/phone/ you are not redirected to the index.php page. No idea why. This is my server configuration.

server {
        listen       80;
        listen       [::]:80 default_server;
        server_name  localhost;
        charset UTF-8;
        index index.html index.php;
        location / {
            root   xyz;
            proxy_set_header Connection "";
            if ($request_uri ~ ^/(.*)\.html$) {  return 302 /$1;  }
            try_files $uri $uri/ $uri.html $uri.php?$args;
            rewrite ^/(.*)/$ /$1 permanent;
            location ~*  \.(jpg|jpeg|png|gif|ico|css|js|ttf|webp)$ {
                expires 365d;
            }
       }

I tried to point the directory and added defaults for index.php and it works, but now all the url's inside /phone/ are showing blank.

location /phone {
   index index.html index.php;
}

By "showing blank" means that they show a 404 error according to the error log

2019/05/08 23:40:58 [error] 26240#14076: *1 CreateFile() "C:\xyz\nginx/xyz/phone/index" failed (2: The system cannot find the file specified), client: ::1, server: localhost, request: "GET /phone/index HTTP/1.1", host: "localhost"

Now the index.php shows a 404 error, what am I doing wrong? Even though the file is there

1 Answers

1
Richard Smith On Best Solutions

If you want to avoid a trailing / for all URIs, but still test for index.html and index.php, you should avoid using the index directive and the $uri/ term with the try_files directive.

One approach is to use a named location to test for PHP files. This location will need your fastcgi configuration.

For example:

root /path/to/root;

location / {
    if ($request_uri ~ ^/(.*)\.html$) { return 302 /$1; }
    rewrite ^/(.*)/$ /$1 permanent;

    try_files $uri $uri.html $uri/index.html @php;
}
location @php {
    try_files $uri.php $uri/index.php =404;
    fastcgi_pass ...;
    ...
}