当前位置: 移动技术网 > IT编程>开发语言>PHP > php 友好URL的实现(吐血推荐)

php 友好URL的实现(吐血推荐)

2019年05月10日  | 移动技术网IT编程  | 我要评论
友好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);
?>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网