框架 TP
由于TP是单入口访问模式,这里不得不借助于PHP的动态处理。
假设, 一个域名 example.com 用户二级域名形如 user1.example.com user2.example.com ......
要求以userX.example.com 访问的时候,绑定到 member 模块下的 index 方法。
首先,在nginx中需要如下配置:
server{
listen 80;
#server_name泛解析绑定
server_name *.example.com;
if ( $host ~ ^(.+).example.com ) {
#从域名中提取出用户名,设置为$user变量,下文使用
set $user $1;
#将模块名和操作名通过get参数指定,这样TP那边就不会根据pathinfo去重新解析模块操作名,将用户名通过 user参数指定,在tp中处理。
rewrite (.*) $1?m=member&a=index&user=$user break;
}
}
TP中 /member/index 下,通过
$user=$_GET['user'];//获取用户名
//根据用户名取该用户的数据,过程省略。
....
访问http://user1.example.com/ftype/1
实际上等于
http://user1.example.com/ftype/1?m=member&a=index&user=user1
这里有个ftype额外的参数,可以通过tp的route路由处理
路由规则如下:
"ftype/([^\/])+" => "member/index?ftype=:1"
TP处理后,
会自动将 ftype参数合并到$_GET中,直接在member/index 中通过$_GET['ftype'] 调用即可。
url等价于
http://user1.example.com/index.php?m=member&a=index&user=user1&ftype=1
当然,在3.X版本中,也许还可以通过$ftype=$_GET["_URL_"][1]; 来取得,就不要路由了。我个人不太推荐这种办法,如果ftype在url中的位置发生变化,将取不到值。
以上是我个人的一点探索,如果谁有更好的解决方案,请务必不吝赐教,谢谢!
最佳答案
