在Nginx上重写对PHP脚本的所有请求

时间:2016-08-11 13:01:49

标签: php apache .htaccess mod-rewrite nginx

我是Nginx的新手,对于我的项目,我需要重写对index.php的所有请求,这是请求处理程序。 我用Apache中的.htaccess文件完成了这个,但现在我想让它与Nginx兼容。我知道在Nginx上不存在.htaccess文件所以我必须编辑/ etc / nginx / sites-available / default中的虚拟主机文件,但我不知道哪个是我的旧Apache规则的Nginx等价物。 这是我旧的.htaccess文件的内容:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^?]*)$ /index.php [QSA,NS,L]

在Apache上将所有请求重写为index.php,无论这些是PHP脚本,静态文件还是不存在的文件(在我的项目中还有一些虚拟路径,如/ :: API /或/ :: ADMIN /和root shorcut /〜/),我怎么能用Nginx做到这一点?

1 个答案:

答案 0 :(得分:1)

nginx文档列出了重写指令:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

简单的答案是这样的:

location / {
    rewrite ^(.*)$ /index.php?$1 last;
}

编辑添加?$ 1位。谢谢,@ bob0t