Apache就没有这个问题,因为Apache是可以直接赋值的。主要原因还是在Nginx,所以直接修改Nginx,conf就可以了。
首先在location / 设置重写,修改完应该如下
location / {
root D:/wwwroot;
index index.html index.htm index.php;
#加入这条语句
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php/$1;
}
}
这样,Nginx找不到的路径都会重写发送到index.php的后面。
接下来设置fastcgi部分,原来的代码是这样的
location ~ \.php {
root x:/wwwroot;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
加入两条语句,将URL中,文件后面的部分赋值到环境变量pathinfo中。更改后的代码如下
location ~ \.php {
root d:/wwwroot;
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_split_path_info ^(.+\.php)(.*)$;
fastcgi_param PATH_INFO $fastcgi_path_info;
include fastcgi_params;
}
接下来还需要修改一下php.ini中的pathinfo设置
cgi.fix_pathinfo = 1
这样就全部完成了,我们直接访问http://localhost/controller/function/argument这样的地址,Nginx自动帮我们重写到http://localhost/index.php/controller/function/argument这个地址,然后就php就可以通过$_SERVER['PATH_INFO']获得参数。