Nginx:将所有不存在的请求重定向到index.php

Mar*_*anc 8 php nginx url-rewriting

我正在从Apache移植到Nginx.

我目前正在使用自定义内容管理解决方案,该解决方案利用SERVER ['request_uri']来处理路由.

我想要做的是将所有不存在的文件和目录请求重定向到/index.php,而不是更新客户端uri.但是,当文件存在时,我想返回它.

一个示例网址是:localhost/content/page/1 < - 应该将$ _SERVER ['request_uri']填充为/ content/page/1或localhost/public/script/exists.js < - 应该作为实际返回文件.

Phe*_*yne 10

您需要在nginx vhost文件中添加location /块或更新当前location /块.

index.php如果找不到文件或目录,这会将所有请求重定向到:

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

这包含在您的服务器指令中,有关更多信息,请访问http://wiki.nginx.org/HttpCoreModule

修改vhost文件后,需要重新启动nginx

注意:服务器块的try_files指令在0.7.44中添加