首先从sef的使用谈起。在
mambo里,页面中要使用sef功能,应该使用sefRelToAbs函数,假设有个
http://www.simyi.com/index.php?option=com_multidisplay&task=view&id=462要使用sef功能,代码里应是这样:
sefRelToAbs("index.php?option=com_multidisplay&task=view&id=462”)
经过mod_rewrite后变成:
http://www.simyi.com/component/option,com_multidisplay/task,view/id,462/
可是我们习惯url的末尾有“.html”。这个可以通过hoping提供的方法:
修改mambo/includes/sef.php
在return $mosConfig_live_site."/".$string;
的上一行添加如下代码:
$string=substr($string,0,-1);
$string.=".html";
这个方法确实简单,经过这样改动后,上面的url就变成了:
http://www.simyi.com/component/option,com_multidisplay/task,view/id,462.html
url友好了许多,但是仔细分析,会发现一些问题。假如上面代码里的链接没有使用sefRelToAbs函数,结果就变成如下:
http://www.simyi.com/index.php?option=com_multidisplay&task=view&id=462.html
这显然是错误的链接,从修改的代码分析,原因在于这两句代码
$string=substr($string,0,-1);
$string.=".html";
它把所有的url都在末尾添加了”.html”,而不管该url是否使用了sefRelToAbs。
结果会造成网站里的登陆模块,用户注册,搜索,pathway导航都出错,
注册链接变成:
http://www.simyi.com/index.php?option=com_registration&task=registe.html
毫无疑问会出错。
如何改进?
Sef.php代码可以改成如下:
if (strstr( $string,"component/" )!="" || strstr( $string,"content/" )!="" ){
$string=substr($string,0,-1);
$string.=".html";
}
return $mosConfig_live_site."/".$string;
因为经过sefRelToAbs和mod_rewrite作用后的url都包含"component/"或"content/"
我们只在这些url的末尾加上”.html”.
在 function sefParseUrl($register_globals){
下面加上:
$_SERVER['REQUEST_URI'] = str_replace( ".html","",$_SERVER['REQUEST_URI'] );
这与前面正好相反,是把.html去掉,否则在程序中post方法取得的id参数将是462.html,易出错。
经过这样的改进,基本上没发现什么url错误了。测试了登陆模块,用户注册,搜索,pathway和在线调查都没发生错误。
解决了.html问题,我还是觉得url看起来很别扭,主要就是其中的“,”,好在这个改起来比较简单。改三个地方。
$temp = explode(",", $value) (有两句)改成
$temp = explode("-", $value)
$string = str_replace( '=', ',', $sefstring ); 改成
$string = str_replace( '=', '-', $sefstring );
即可实现如下的url:
http://www.simyi.com/component/option-com_multidisplay/task-view/id-462.html
其中的“-”通过改动上面的代码可设定成自己喜欢的,但要注意参数中不能出现相同的符号。如id-46-2.html这样会出错的。
经过这样的修改,可以说得到了一个对于搜索引擎、浏览者都很友好的url。
本程序演示地址-我的主页:
http://www.simyi.com
写此篇文章在于抛砖引玉,体会 mambo的灵活强大的功能,若本文有错误之处,欢迎指正。
附上修改过的sef.php,下载后覆盖includes/sef.php 即可,记得覆盖前先做好备份。
[
本帖最后由 shunzi34 于 2007-6-24 12:21 编辑 ]