友好url的实现(吐血推荐)
大家经常看到别的站的url是这样的吧?
http://www.xxx.com/module/show/action/list/page/7
或者
http://xx.com/module/show/action/show/id/8.shtml 带扩展名的
或者
http://xx.com/module/show/action/show/id/8?word=ss&age=11
这样的吧
今天我就是公布下这种方法的实现,并独立出最简单的代码
函数如下,没封装成类,主要是没必要,用函数能方便些
<?php
/**
* 获得友好的url访问
*
* @access public
* @return array
*/
function getquerystring(){
$_sgets = explode("/",substr($_server['path_info'],1));
$_slen = count($_sgets);
$_sget = $_get;
for($i=0;$i<$_slen;$i+=2){
if(!empty($_sgets[$i]) && !empty($_sgets[$i+1])) $_sget[$_sgets[$i]]=$_sgets[$i+1];
}
$_sget['m'] = !empty($_sget['m']) && is_string($_sget['m']) ? trim($_sget['m']).'action' : 'indexaction';
$_sget['a'] = !empty($_sget['a']) && is_string($_sget['a']) ? trim($_sget['a']) : 'run';
return $_sget;
}
/**
* 生成链接url
*
* @access public
* @param array $arr
* @return string
*/
function seturl($arr){
global $global;
$querystring='';
if($global['urlmode']==2){
foreach($arr as $k=> $v){
$querystring.=$k.'/'.$v.'/';
}
}
$querystring.=$global['urlsuffix'];
return $querystring;
}
?>
使用很简单
<?php
$_get= getquerystring();
?>
但是这样还不行,这样只能实现
http://www.xxx.com/index.php/module/show/action/list/page/7 这样的
中间多了个index.php 为此我们要把他去掉,只好重写
但是有些文件 又不希望这样,比如 样式 图片,那就放条件里
建立一个 .htaccess文件
rewriteengine on
rewritecond $1 !^(index\.php|css|pics|themes|js|robots\.txt)
rewriterule ^(.*)$ index.php/$1 [l]
现在ok了,赶快去测试吧
<?php
$_get= getquerystring();
print_r($_get);
?>
如对本文有疑问,
点击进行留言回复!!
相关文章:
-
-
-
-
-
-
-
-
-
管理后台无限级菜单
管理后台菜单遍历很久就想做一个通用的管理后台出来,一直太懒没做,今天开始粗略搞了一下,只是先把框架搭建好,菜单栏...
[阅读全文]
-
桥接模式学习总结
桥接模式引言假如一个系统需要加入报表模块,主要的开发点是数据的获取和报表的显示方式。数据的获取可能是从系统数据库...
[阅读全文]
-
网友评论