php伪静态函数,PHP写的U()函数,结合伪静态做seo优化

刚完成开发的网站上线的第一件事就是做seo优化,例如添加网站内容、定制页面关键词、交换网站的友情链接等相关的工作。这里,我重点说的是站内链接的优化,简洁统一的站内链会对访问网站的用户很友好,用户体验好了,相信搜索引擎会更喜欢,这就是伪静态地址的一个好处。

PHP网站在开发的过程,基本上使用的都是动态地址类似:http://www.xxx.com/xxx.php?xx=xx&xx=xx,为了加速搜索引擎的收录速度,最好在网站正式上线前,把动态地址更换为伪静态的方式:http://www.xxx.com/xx/xx/,这是一个优化细节。换个方式思考一下,电脑上的资料,是分类存放比较好找,还是随意存放?

很多时候,由于网站的动态地址太多了,所以在做伪静态的时候,需要修改的链接地址很多,工作量很大。所以我自己基于PHP写了一个U()函数,省去了在使用mPHP开发过程中不必要的麻烦。下边直接给出使用方法,和代码实现。<?php

$urlA = "index.php?c=article&a=view&id=1&page=1";

$urlB = "?c=article&a=view&id=1&page=2";

$urlC = "c=article&a=view&id=3&page=1";

$urlD = "http://www.xxx.com/index.php?c=test&a=index";

echo "\$urlA = $urlA
";

echo "\$urlB = $urlB
";

echo "\$urlC = $urlC
";

echo "\$urlD = $urlD
";

echo '没有配置参数的链接地址
';

echo 'U($urlA) = ',U($urlA),'
';

echo 'U($urlB) = ',U($urlB),'
';

echo 'U($urlC) = ',U($urlC),'
';

echo 'U($urlD) = ',U($urlD),'
';

$CFG['url_type'] = 'DIR';//伪静态地址类型,DIR:目录类型,NODIR:无目录类型

$CFG['url_depth'] = 1;//伪静态地址目录深度,超出后用-来连接,

$CFG['url_suffix'] = '.html';//伪静态地址后缀

echo '配置参数的链接地址
';

echo 'U($urlA) = ',U($urlA),'
';

echo 'U($urlB) = ',U($urlB),'
';

echo 'U($urlC) = ',U($urlC),'
';

echo 'U($urlD) = ',U($urlD),'
';

$CFG['url_depth'] = 2;

echo '更改目录深度为2
';

echo 'U($urlA) = ',U($urlA),'
';

输出结果:$urlA = index.php?c=article&a=view&id=1&page=1

$urlB = ?c=article&a=view&id=1&page=2

$urlC = c=article&a=view&id=3&page=1

$urlD = http://www.xxx.com/index.php?c=test&a=index

没有配置参数的链接地址

U($urlA) = http://localhost/?c=article&a=view&id=1&page=1

U($urlB) = http://localhost/?c=article&a=view&id=1&page=2

U($urlC) = http://localhost/?c=article&a=view&id=3&page=1

U($urlD) = http://www.xxx.com/?c=test&a=index

配置参数的链接地址

U($urlA) = http://localhost/article/view-1-1.html

U($urlB) = http://localhost/article/view-1-2.html

U($urlC) = http://localhost/article/view-3-1.html

U($urlD) = http://www.xxx.com/test/index.html

更改目录深度为2

U($urlA) = http://localhost/article/view/1-1.html

U()函数代码实现:/*

作者:moyancheng

最后更新时间:2013-05-17

最后更新时间:2013-4-3

$strUrl:动态地址

$true:生成的伪静态地址是否加后缀,默认添加

*/

function U($strUrl = '',$true = true) {

global $CFG;

$arrGet = $arrVal =  $arrData =  array();

$intDepth = 0;

$intDepthMax = $CFG['url_depth'];

$arrData = array(

'?' => '',

'index.php' => ''

);

$strUrl = strtr($strUrl,$arrData);

if( substr($strUrl,0,7) == 'http://' || substr($strUrl,0,8) == 'https://' ) {

$leng = strpos($strUrl,'/',7);

$url = substr($strUrl,0,$leng).'/';

$strUrl = strtr($strUrl,array($url => ''));

} else {

$url = "http://{$_SERVER['SERVER_NAME']}/";

}

if($CFG['url_type'] == 'DIR') $flag = '/';

elseif($CFG['url_type'] == 'NODIR') $flag = '-';

else {

if($strUrl[0] != '?') $strUrl = "{$url}?{$strUrl}";

return $strUrl;

}

$arrData = explode('&',$strUrl);

foreach( $arrData as $str) {

$arrVal = explode('=',$str);

$arrGet[$arrVal[0]] = $arrVal[1];

}

$strUrl = '';

foreach($arrGet as $key => $val) {

if( $intDepth 

$strUrl .= "{$val}{$flag}";

++$intDepth;

} else {

$strUrl .= "{$val}-";

}

}

$strUrl =  $url.trim($strUrl,'/-');

if($true)$strUrl .= $CFG['url_suffix'];

unset($CFG);

return $strUrl;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值