php伪静态写法--其一
伪静态又名:url重写
主要是为了seo而生的。(seo是什么?这个不用问我吧。呵呵~搞网络的不懂seo那就~~~~)
方法一:
比如这个网页
http://www.2cto.com /soft.php/1,100,8630.html
其实处理的脚本是soft.php 参数为1,100,8630
相当于soft.php?a=1&b=1=100&c=8630 只不过这样的url太难记。搜索引擎也不喜欢。
真静态只是完全生成了html。
客户端访问的时候直接输出。不用脚本解释。在流量非常大的时候(比如每天有上百万的访问量的时候)会起到很好的效果。也就是说服务器端实实在在的存在这个html页面。
当然在你网站的流量没有那么大的时候。url重写是最好的方法(个人观点,大流量的时候可以考虑负载均衡了。同样没有关系)
附url重写的方法有很多种,apache,iisrewrite。甚至php脚本都可以直接处理。比如上例中就是php脚本直接处理(该方法好处是大流量的时候直接减轻web伺服器的压力。ps:同样也是个人观点:
================================================
下面以程序为例讲一下php伪静态的程序实现方法,其实这方法我之前已经有在其它论坛社区发过
程序为例:
http://www.2cto.com /soft.php/1,100,8630.html
code:
//利用server变量 取得path_info信息 该例中为 /1,100,8630.html 也就是执行脚本名后面的部分
if(@$path_info =$_server["path_info"]){
//正则匹配一下参数
if(preg_match("/\/(\d+),(\d+),(\d+)\.html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
}else die("path:error!");
//相当于soft.php?gid=1&sid=100&softid=8630
//就是这么简单了。~)
方法二:
一 打开 apache 的配置文件 httpd.conf 。
二 将#loadmodule rewrite_module modules/mod_rewrite前面的#去掉
三 在 httpd.conf中添加:
rewriteengine on
#rewritecond %{env:script_url} (?:index|dispbbs)[-0-9]+.html
rewriterule ^(.*?(?:index|dispbbs))-([-0-9]+).html 1.php?__is_apache_rewrite=1&__rewrite_arg=2
四 要实现asp帖子url到php帖子的映射,在 第三步的
rewritemap tolowercase int:tolower
rewritecond %{query_string} (?:boardid|page|id|replyid|star|skin)=d+ [nc]
rewriterule ^(.*(?:index|dispbbs)).asp 1.php?{tolowercase:%{query_string}}&__is_apache_rewrite=1
五 保存httpd.conf并重启apache
方法三:
<?php /* 功能:PHP伪静态化页面的实现 具体用法: 例如链接为:test.php/year/2006/action/_add.html mod_rewrite(); $yearn=$_GET["year"];//结果为'2006' $action=$_GET["action"];//结果为'_add' */ function mod_rewrite(){ global $_GET; $nav=$_SERVER["REQUEST_URI"]; $script_name=$_SERVER["SCRIPT_NAME"]; $nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1); $nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm $vars = explode("/",$nav); for($i=0;$i<Count($vars);$i+=2){ $_GET["$vars[$i]"]=$vars[$i+1]; } return $_GET; } mod_rewrite(); $yearn=$_GET["year"];//结果为'2006' $action=$_GET["action"];//结果为'_add' echo $yearn; echo $action; ?>
<?php /* 功能:PHP伪静态化页面的实现 具体用法: 例如链接为:test.php/year/2006/action/_add.html mod_rewrite(); $yearn=$_GET["year"];//结果为'2006' $action=$_GET["action"];//结果为'_add' */ function mod_rewrite(){ global $_GET; $nav=$_SERVER["REQUEST_URI"]; $script_name=$_SERVER["SCRIPT_NAME"]; $nav=substr(ereg_replace("^$script_name","",urldecode($nav)),1); $nav=preg_replace("/^.ht(m){1}(l){0,1}$/","",$nav);//这句是去掉尾部的.html或.htm $vars = explode("/",$nav); for($i=0;$i<Count($vars);$i+=2){ $_GET["$vars[$i]"]=$vars[$i+1]; } return $_GET; } mod_rewrite(); $yearn=$_GET["year"];//结果为'2006' $action=$_GET["action"];//结果为'_add' echo $yearn; echo $action; ?>