douba9020 2016-08-11 13:01
浏览 41
已采纳

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

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]

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?

  • 写回答

1条回答 默认 最新

  • doushi1996 2016-08-11 13:06
    关注

    The nginx docs lay out the rewrite directive: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

    The simple answer is something like this:

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

    Edited to add the ?$1 bit. Thanks, @bob0t

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于这个复杂问题的解释说明
  • ¥50 三种调度算法报错 采用的你的方案
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败