代码改变世界

Apache结合Thinkphp实现伪静态的设置方法

2011-05-24 15:04  卫佳  阅读(2943)  评论(0编辑  收藏  举报

1、要隐藏Thinkphp项目中index.php文件名,需在config.php中设置'URL_MODEL'=>2,并且需要在.htaccess 中隐藏index.php,前者依赖于后者,否则可能导致Thinkphp项目中模块无法正常访问。

2、设置了'URL_MODEL'=>2,并且在.htaccess也隐藏了index.php,普通的查看记录的URL就变为了这种形式:

http://localohost/News/view/id/45.html

复制代码

但这并不能满足我们的URL优化效果,因为更好的SEO优化地址可能是这种形式:

http://localohost/News/2009-11-04/45.html

cufflinks wholesale这就需要我们更好的手动的去做为静态了。

经过测试,以下方式不可取:
1、'URL_MODEL'=>2、.htaccess隐藏index.php,

添加Rewrite规则:

RewriteRule ^News/(.*)/([0-9]+)\.html$ /News/view/id/$2

复制代码

结果提示找不到模块,并且在根目录下的Discuz论坛为静态也失效,看来隐藏index.php后果很严重。

2、'URL_MODEL'=>2、.htaccess不隐藏index.php,这样都矛盾了,更不行了。
3、'URL_MODEL'=>1、.htaccess隐藏index.php,这样项目生产的路径无法找到模块,论坛和其他文件夹伪静态也失效了。

最后总结出一种成功的办法:
1、'URL_MODEL'=>1,不隐藏index.php,再在.htaccess添加规则:

RewriteRule ^News/$     index.php/News/

RewriteRule ^News/category([0-9]+).html$   index.php/News/category/classid/$1

RewriteRule ^User/$        index.php/User/

RewriteRule ^News/(.*)/([0-9]+)\.html$    index.php/News/view/id/$2

……

discount nike air shoes
需要说明的是,URL转发地址要与'URL_MODEL'中设置对应,如果设置的URL_MODEL=0,那么转发地址就要相应的变为

RewriteRule ^News/category([0-9]+).html$   index.php?m=News&action=category&classid=$1

复制代码


形式。

这样,一下的经过优化的URL就可以正常访问了,也不印象其他的程序。

http://localhost/News/
http://localhost/News/category2.html
http://localhost/User/
http://localhost/News/2009-11-05/22.html

也就是说,不利用'URL_MODEL'=>2(设置后,“__APP__”就会自动隐藏index.php路径),不隐藏index.php,我们就可以更加灵活的控制URL 优化了。

有不对的地方请大家指正。