在ThinkPHP框架下配置nginx实现URL伪静态需要以下步骤:
location / {
if (!-e $request_filename){
rewrite ^/(.*)$ /index.php?s=/$1 last;
break;
}
}
其中,location /表示匹配所有请求路径;if (!-e $request_filename)表示如果请求的文件不存在;rewrite ^/(.*)$ /index.php?s=/$1 last;表示将所有请求重写到index.php文件,并附带s参数,last表示终止rewrite操作;break;表示终止当前的location处理。
config.php,将URL模式改为1,即采用PATHINFO模式:'URL_MODEL' => '1',
这样配置后,就可以使用伪静态URL访问ThinkPHP应用了。例如,原来的URL为http://example.com/index.php/Index/index/id/1.html,经过伪静态后,可以访问http://example.com/Index/index/id/1.html,URL中的.html后缀也可以去掉。
需要注意的是,如果使用的是子目录部署,还需要在nginx配置中加入以下代码:
location /子目录名称 {
if (!-e $request_filename){
rewrite ^/子目录名称/(.*)$ /子目录名称/index.php?s=/$1 last;
break;
}
}
其中,子目录名称需要替换成实际的子目录名称。