douba9020 2016-08-11 05: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 05: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 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部