将所有请求重写为Nginx上的PHP脚本 [英] Rewrite all requests to PHP script on Nginx
问题描述
我是Nginx的新手,对于我的项目,我需要将所有请求重写为index.php,即请求处理程序. 我在Apache中使用.htaccess文件完成了此操作,但现在我想使其也与Nginx兼容.我知道Nginx上不存在.htaccess文件,因此我必须在/etc/nginx/sites-available/default中编辑虚拟主机文件,但是我不知道Nginx等效于我的旧Apache规则. 这是我旧的.htaccess文件的内容:
Im new with Nginx and for my project I need to rewrite all requests to the index.php, which is the request handler. I done this with .htaccess file in Apache but now I wanted to make it compatible also with Nginx. I know that on Nginx don't exist .htaccess files so I have to edit the virtual host file in /etc/nginx/sites-available/default but I dont know which is the Nginx equivalent of my old Apache rules. Here's the content of my old .htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/index\.php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^([^?]*)$ /index.php [QSA,NS,L]
在Apache上,所有请求都被重写为index.php,无论这些请求是PHP脚本,静态文件还是不存在的文件(在我的项目中,也存在一些虚拟路径,例如/:: API/或/:: ADMIN /和根目录shorcut/〜/),如何使用Nginx做到这一点?
With this on Apache all requests was rewritten to index.php, no matter if these are PHP scripts, static files or inexistent files (in my project there are also some virtual paths like /::API/ or /::ADMIN/ and the root shorcut /~/), how can I do that with Nginx?
推荐答案
nginx文档列出了重写指令: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
The nginx docs lay out the rewrite directive: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html
简单的答案是这样的:
location / {
rewrite ^(.*)$ /index.php?$1 last;
}
编辑后添加?$ 1位.谢谢@ bob0t
Edited to add the ?$1 bit. Thanks, @bob0t
这篇关于将所有请求重写为Nginx上的PHP脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!