如何将所有请求重定向到index.php,即使url中包含.php

Gia*_*año 5 nginx

我有这个 nginx 虚拟主机配置:

server {
        listen 8081;
        server_name blocked_server;
        root /home/gian/blocked_server;
        access_log off;
        error_log off;
        index index.php index.html index.htm index.nginx-debian.html;

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

        location ~ / {
                try_files $uri $uri /index.php;
        }
}
Run Code Online (Sandbox Code Playgroud)

它将所有 url 重定向到 index.php,除非 url 以 .php 扩展名结尾。

例如,这有效: http://localhost/somethingthatdontexist

但这会返回 404 并且不会重定向到index.php http://localhost/somethingthatdontexist.php

如何将不存在的 .php 扩展名的 url 重定向到 index.php?

Gia*_*año 2

我已经解决了这个问题。首先,您应该注释掉这一行或从snippets/fastcgi-php.conf文件中删除这一行

try_files $fastcgi_script_name =404;
Run Code Online (Sandbox Code Playgroud)

然后将您的虚拟主机配置放在块try_files $uri $uri /index.php;之前。include snippets/fastcgi-php.conf;location ~\.php$

location ~\.php$块应该如下所示:

location ~ \.php$ {
    try_files $uri $uri /index.php;
    include snippets/fastcgi-php.conf;
    fastcgi_pass unix:/run/php/php7.0-fpm.sock;
}
Run Code Online (Sandbox Code Playgroud)

这应该够了吧。