我正在将我的服务器从Apache迁移到Nginx并拥有这个非常简单的.htaccess规则:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
它背后的想法是将每个请求都指向前端控制器(index.php).我正在尝试用Nginx做同样的事情.我使用在线转换器来制作这个Nginx位置块:
location / {
if (!-e $request_filename){
rewrite ^(.*)$/index.php break;
}
}
但是当我将它添加到我的网站的配置时,Nginx只是下载PHP文件的源代码.作为参考,这是整个配置文件:
我知道PHP有效,好像我删除了位置块并使用<?php phpinfo()创建了一个文件;它工作正常. 任何帮助,将不胜感激.
最佳答案
这就是我将所有行为路由到index.php的方式,包括子目录请求,HTTP args等.
location / {
try_files $uri $uri/ /index.php?$args; #if doesn't exist,send it to index.php
}
location ~ \.php${
include fastcgi_params;
fastcgi_intercept_errors on;
# By all means use a different server for the fcgi processes if you need to
fastcgi_pass 127.0.0.1:9000;
}
例如,这些被发送到index.php:
http://foo.bar/something/
http://foo.bar/something/?something=1
虽然这些直接转到文件
http://foo.bar/someotherphp.php
http://foo.bar/assets/someimg.jpg
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。