h"/> h">

 >  기사  >  백엔드 개발  >  PHP友好URL的实现_PHP教程

PHP友好URL的实现_PHP教程

WBOY
WBOY원래의
2016-07-13 17:35:151090검색

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

大家经常看到别的站的URL是这样的吧?
http://www.xxx.com/module/show/action/list/page/7
或者
html">http://xx.com/module/show/action/show/id/8.shtml 带扩展名的
或者
http://xx.com/module/show/action/show/id/8?word=ss&age=11
这样的吧
今天我就是公布下这种方法的实现,并独立出最简单的代码
函数如下,没封装成类,主要是没必要,用函数能方便些

/**
* 获得友好的URL访问
*
* @accesspublic
* @return array
*/
function getQueryString(){
$_SGETS = explode("/",substr($_SERVER[PATH_INFO],1));
$_SLEN = count($_SGETS);
$_SGET = $_GET;
for($i=0;$iif(!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
*
* @accesspublic
* @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;
}
?>
使用很简单
$_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了,赶快去测试吧

$_GET= getQueryString();
print_r($_GET);
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/508354.htmlTechArticle下面的代码主要是伪静态的实现,搜索引擎喜欢 大家经常看到别的站的URL是这样的吧? http://www.xxx.com/module/show/action/list/page/7 或者 html">h...
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.