再做一个 TP 项目的二次开发的时候,遇到了一种这样的路由解析情况,需要 nginx 配置重写规则
- 原路由
news/show_10_12.html
news/index.php?m=index&a=show&catid=10&id=12
- 对应的 nginx 重写规则
location / {
try_files $uri $uri/ /index.php?$query_string;
#rewrite /news/show_(.+?)_(.+?)\.html$ /news/index.php?m=index&c=index&a=show&catid=$1&id=$2 last;
rewrite show_([0-9]+)_([0-9]+).html /news/index.php?m=index&c=index&a=show&catid=$1&id=$2 last;
rewrite list_([0-9]+)_([0-9]+).html index.php?m=index&c=index&a=lists&catid=$1&page=$2;
rewrite list_([0-9]+).html index.php?m=index&c=index&a=lists&catid=$1;
}
推荐
TP 重写配置
根目录,ceshi,项目文件夹是 test 。现在得情况是整个tp项目是放在 test 文件夹下得,ceshi 是根目录.需要实现得是 正常访问
/Home/index/index
,/test/Home/index/index
,就能够实现和访问/index.php?c=index&a=index
得到一样得效果
配置中
'URL_MODEL' => 2, // URL访问模式,可选参数0、1、2、3,代表以下四种模式:
// 0 (普通模式); 1 (PATHINFO 模式); 2 (REWRITE 模式); 3 (兼容模式) 默认为PATHINFO 模式
location / {
if (!-e $request_filename){
# test 开头,则需要重定向到 test/index.php 这个是 tp 的入口文件。这个的目的是处理去掉 test
rewrite ^/test/(.*)$ /test/index.php?s=$1 last; # (.*) 代表的是 $1
# 没有 test 开头,则需要重定向到 test/index.php 这个是 tp 的入口文件
# /Home/index/index 直接指向对应的模块,控制器和方法
rewrite ^(.*)$ /test/index.php?s=$1 last;
}
}