A-A+

PHP友好URL的实现

2013年07月25日 PHP技术文章 暂无评论 阅读 56 views 次

下面的代码主要是伪静态的实现,搜索引擎喜欢

大家经常看到别的站的URL是这样的吧?

https://www.xiariboke.net/module/show/action/list/page/7

或者

html">http://xiariboke.net/module/show/action/show/id/8.shtml 带扩展名的

或者

http://xx.com/module/show/action/show/id/8?word=ss&age=11

这样的吧,今天我就是公布下这种方法的实现,并独立出最简单的代码,函数如下,没封装成类,主要是没必要,用函数能方便些

<?php
/**
* 获得友好的URL访问
*
* @accesspublic
* @return array
*/
function getQueryString(){
$_SGETS = explode("/",substr($_SERVER&#91;PATH_INFO&#93;,1));
$_SLEN = count($_SGETS);
$_SGET = $_GET;
for($i=0;$i<$_SLEN;$i+=2){
if(!empty($_SGETS&#91;$i&#93;) && !empty($_SGETS&#91;$i+1&#93;)) $_SGET&#91;$_SGE
TS&#91;$i&#93;&#93;=$_SGETS&#91;$i+1&#93;;
}
$_SGET&#91;m&#93; = !empty($_SGET&#91;m&#93;) && is_string($_SGET&#91;m&#93;) ? tri
m($_SGET&#91;m&#93;).Action : indexAction;
$_SGET&#91;a&#93; = !empty($_SGET&#91;a&#93;) && is_string($_SGET&#91;a&#93;) ? tri
m($_SGET&#91;a&#93;) : run;
return $_SGET;
}
/**
* 生成链接URL
*
* @accesspublic
* @param array $arr
* @return string
*/
function setUrl($arr){
global $Global;
$queryString=;
if($Global&#91;urlmode&#93;==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);
?>
标签:

给我留言