手机浏览 RSS 2.0 订阅 膘叔的简单人生 , 腾讯云RDS购买 | 超便宜的Vultr , 注册 | 登陆

Yii Rewrite中遇到的困惑

首页 > PHP Framework >

用Yii开发的时候,总是会有涉及到rewrite,否则,网址里一堆 index.php?r=xxxx那就真的太长了。

关于rewrite,可以参考官方的guide,不过上面只有针对apache的htaccess配置。如果是nginx,你还可以搜索一下官方的wiki,有介绍nginx的配置的。只是官方上面用的方法是try_file,只针对高版本的。如果是低版本还是得用 if (!e $request_filename)这类的【更多设置请搜索google】我这里不详细说了。

今天在用官方推出的这一种rewrite功能【Parameterizing Hostnames 】的时候,出了点问题,该功能是:

XML/HTML代码
  1. Parameterizing Hostnames  
  2.   
  3. Starting from version 1.0.11, it is also possible to include hostname into the rules for parsing and creating URLs. One may extract part of the hostname to be a GET parameter. For example, the URL http://admin.example.com/en/profile may be parsed into GET parameters user=admin and lang=en. On the other hand, rules with hostname may also be used to create URLs with paratermized hostnames.  
  4.   
  5. In order to use parameterized hostnames, simply declare URL rules with host info, e.g.:  
  6.   
  7. array(  
  8.     'http://<user:\w+>.example.com/<lang:\w+>/profile' => 'user/profile',  
  9. )  
  10.   
  11. The above example says that the first segment in the hostname should be treated as user parameter while the first segment in the path info should be lang parameter. The rule corresponds to the user/profile route.  
  12.   
  13. Note that CUrlManager::showScriptName will not take effect when a URL is being created using a rule with parameterized hostname.  
  14.   
  15. Also note that the rule with parameterized hostname should NOT contain the sub-folder if the application is under a sub-folder of the Web root. For example, if the application is under http://www.example.com/sandbox/blog, then we should still use the same URL rule as described above without the sub-folder sandbox/blog.  

我在rules里写上了 "http://user.xxx.com/<_c:\w+>/<_a:\w+>"=>"user/<_c>/<_a>/"

然后发现访问 http://user.xxx.com/aaa/bbb/id/1的时候出现了404错误,找问题死活找不到在哪里。再转过头去看了很多其他的配置,发现都不正常。

然后,我尝试访问默认页,即没有参数的:http://user.xxx.com/default/index,咦,居然正常了。。但问题随之而来,其他链接都不正常,经过不停的测试测试再测试。最终发现,把""user/<_c>/<_a>/" 中最后一个 / 去掉就正常了。

那么,'<_m:\w+>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>/'  和  '<_m:\w+>/<_c:\w+>/<_a:\w+>' => '<_m>/<_c>/<_a>' 的区别是什么呢?

仔细看了一下生成的链接,如果用第二种的时候,如果有多个参数,就转为 m/c/a?a=b&c=d 而如果用第一种就是生成 m/c/a/a/b/c/d 。这时候,系统可能会识别不出我这种用二级域名做rewrite的方式。于是,我去掉了最后一个“/”,一切都正常了。

期间,HM和Horadric.Cube多次对我帮助,告诉我不同的nginx参数的配置。灰常感谢。




本站采用创作共享版权协议, 要求署名、非商业和保持一致. 本站欢迎任何非商业应用的转载, 但须注明出自"易栈网-膘叔", 保留原始链接, 此外还必须标注原文标题和链接.

Tags: yii, rewrite, rules, route, htaccess

« 上一篇 | 下一篇 »

只显示10条记录相关文章

常用网站的反向代理页[2013-09-28] (浏览: 66064, 评论: 10)
Yii CDbCriteria的常用方法 (浏览: 56342, 评论: 5)
将Yiiframework与JQuery easyUI整合使用 (浏览: 38198, 评论: 2)
Yii:relations update(self::STAT) (浏览: 33673, 评论: 0)
利用.htaccess绑定域名到子目录 (浏览: 30139, 评论: 2)
值得收藏的yii2的doc中关于db Query的说明 (浏览: 29239, 评论: 0)
Yii Demos 随想 (浏览: 28546, 评论: 3)
在Yii框架中使用Hprose或PHPRPC (浏览: 27557, 评论: 0)
Yii ClinkPager 郁闷 (浏览: 27277, 评论: 2)
Yiiframework(Yii框架)开发笔记:续四 (浏览: 26748, 评论: 3)

发表评论

评论内容 (必填):