Joomla中的SEF知识点
Joomla中的SEF知识点
Joomla是目前比较盛行的CMS系统,良好的框架结构使越来越多的开发人员加入进来。使用Joomla有一段时间的人都知道Joomla所生成的站点一般都是基于动态URL的对于搜索引擎来说动态URL的站点并不是就不能搜索到就一定会排在后面而是不太友好,这个不友好也是暂时的目前的个别现象,事物都是进步的GOOGLE也会在不断进步,其实我并不喜欢那些把搜索引擎吹呼的不得了的人,这个优化大师那个优化姥姥的,那些人八成都是有一点点个人的私欲在里面,要是不小心刺激到你,抱歉我不是有意在说你!
Joomla中的SEF说白了就是一个对URL的重写的过程将原来参数众多,层次很深的URL改写为一个简单的更容易被记住被搜索的URL。通过分析Joomla站点的URL结果就会发现规律很明显:
域名+index.php?option=com_content&task=category§ionid=4&id=13&Itemid=27
以上就是一个最普通不过的URL,其中包含的元素有option(组件参数,告诉系统一下内容来自哪个组件)、task(任务参数,组件内执行什么任务上面的例子中代表执行分类列表,sectionid内容的单元号JOOMLA特有,id,itemid项目号)。Joomla本身就自带一个URL优化的组件,也就是一个函数实现对上述地址的重写为index.php/content/view/4/13/27.html,是不是貌似静态,严格来说应该是伪装的静态。
下面的函数sefRelToAbs就是实现上述改写的
/**
*ConvertsanabsoluteURLtoSEFformat
*@paramstringTheURL
*@returnstring
*/
functionsefRelToAbs($string){
global$mosConfig_live_site,$mosConfig_sef,$mosConfig_mbf_content,$mosConfig_multilingual_support;
global$iso_client_lang;
//multilingualcodeurlsupport
if($mosConfig_sef&&($mosConfig_mbf_content||$mosConfig_multilingual_support)&&$string!='index.php'&&!eregi("^(([^:/?#]+):)",$string)&&!strcasecmp(substr($string,0,9),'index.php')&&!eregi('lang=',$string)){
$string.='&lang='.$iso_client_lang;
}
//SEFURLHandling
if($mosConfig_sef&&!eregi("^(([^:/?#]+):)",$string)&&!strcasecmp(substr($string,0,9),'index.php')){
//Replaceall&with&
$string=str_replace('&','&',$string);
//Homeindex.php
if($string=='index.php'){
$string='';
}
//breaklinkintourlcomponentparts
$url=parse_url($string);
//checkiflinkcontainedfragmentidentifiers(ex.#foo)
$fragment='';
if(isset($url['fragment'])){
//ensurefragmentidentifiersarecompatiblewithHTML4
if(preg_match('@^[A-Za-z][A-Za-z0-9:_.-]*$@',$url['fragment'])){
$fragment='#'.$url['fragment'];
}
}
//checkiflinkcontainedaquerycomponent
if(isset($url['query'])){
//specialhandlingforjavascript
$url['query']=stripslashes(str_replace('+','%2b',$url['query']));
//cleanpossiblexssattacks
$url['query']=preg_replace("'%3Cscript[^%3E]*%3E.*?%3C/script%3E'si",'',$url['query']);
//breakurlintocomponentparts
parse_str($url['query'],$parts);
//specialhandlingforjavascript
foreach($partsas$key=>$value){
if(strpos($value,'+')!==false){
$parts[$key]=stripslashes(str_replace('%2b','+',$value));
}
}
//var_dump($parts);
$sefstring='';
//Componentcom_contenturls
if(((isset($parts['option'])&&($parts['option']=='com_content'||$parts['option']=='content')))&&($parts['task']!='new')&&($parts['task']!='edit')){
//index.php?option=com_content[&task=$task][§ionid=$sectionid][&id=$id][&Itemid=$Itemid][&limit=$limit][&limitstart=$limitstart][&year=$year][&month=$month][&module=$module]
$sefstring.='content/';
//task
if(isset($parts['task'])){
$sefstring.=$parts['task'].'/';
}
//sectionid
if(isset($parts['sectionid'])){
$sefstring.=$parts['sectionid'].'/';
}
//id
if(isset($parts['id'])){
$sefstring.=$parts['id'].'/';
}
//Itemid
if(isset($parts['Itemid'])){
//onlyaddItemidvalueifitdoesnotcorrespondwiththe'unassigned'Itemidvalue
if($parts['Itemid']!=99999999&&$parts['Itemid']!=0){
$sefstring.=$parts['Itemid'].'/';
}
}
//order
if(isset($parts['order'])){
$sefstring.='order,'.$parts['order'].'/';
}
//filter
if(isset($parts['filter'])){
$sefstring.='filter,'.$parts['filter'].'/';
}
//limit
if(isset($parts['limit'])){
$sefstring.=$parts['limit'].'/';
}
//limitstart
if(isset($parts['limitstart'])){
$sefstring.=$parts['limitstart'].'/';
}
//year
if(isset($parts['year'])){
$sefstring.=$parts['year'].'/';
}
//month
if(isset($parts['month'])){
$sefstring.=$parts['month'].'/';
}
//module
if(isset($parts['module'])){
$sefstring.=$parts['module'].'/';
}
//lang
if(isset($parts['lang'])){
$sefstring.='lang,'.$parts['lang'].'/';
}
$string=$sefstring;
//allothercomponents
//index.php?option=com_xxxx&...
}elseif(isset($parts['option'])&&(strpos($parts['option'],'com_')
*ConvertsanabsoluteURLtoSEFformat
*@paramstringTheURL
*@returnstring
*/
functionsefRelToAbs($string){
global$mosConfig_live_site,$mosConfig_sef,$mosConfig_mbf_content,$mosConfig_multilingual_support;
global$iso_client_lang;
//multilingualcodeurlsupport
if($mosConfig_sef&&($mosConfig_mbf_content||$mosConfig_multilingual_support)&&$string!='index.php'&&!eregi("^(([^:/?#]+):)",$string)&&!strcasecmp(substr($string,0,9),'index.php')&&!eregi('lang=',$string)){
$string.='&lang='.$iso_client_lang;
}
//SEFURLHandling
if($mosConfig_sef&&!eregi("^(([^:/?#]+):)",$string)&&!strcasecmp(substr($string,0,9),'index.php')){
//Replaceall&with&
$string=str_replace('&','&',$string);
//Homeindex.php
if($string=='index.php'){
$string='';
}
//breaklinkintourlcomponentparts
$url=parse_url($string);
//checkiflinkcontainedfragmentidentifiers(ex.#foo)
$fragment='';
if(isset($url['fragment'])){
//ensurefragmentidentifiersarecompatiblewithHTML4
if(preg_match('@^[A-Za-z][A-Za-z0-9:_.-]*$@',$url['fragment'])){
$fragment='#'.$url['fragment'];
}
}
//checkiflinkcontainedaquerycomponent
if(isset($url['query'])){
//specialhandlingforjavascript
$url['query']=stripslashes(str_replace('+','%2b',$url['query']));
//cleanpossiblexssattacks
$url['query']=preg_replace("'%3Cscript[^%3E]*%3E.*?%3C/script%3E'si",'',$url['query']);
//breakurlintocomponentparts
parse_str($url['query'],$parts);
//specialhandlingforjavascript
foreach($partsas$key=>$value){
if(strpos($value,'+')!==false){
$parts[$key]=stripslashes(str_replace('%2b','+',$value));
}
}
//var_dump($parts);
$sefstring='';
//Componentcom_contenturls
if(((isset($parts['option'])&&($parts['option']=='com_content'||$parts['option']=='content')))&&($parts['task']!='new')&&($parts['task']!='edit')){
//index.php?option=com_content[&task=$task][§ionid=$sectionid][&id=$id][&Itemid=$Itemid][&limit=$limit][&limitstart=$limitstart][&year=$year][&month=$month][&module=$module]
$sefstring.='content/';
//task
if(isset($parts['task'])){
$sefstring.=$parts['task'].'/';
}
//sectionid
if(isset($parts['sectionid'])){
$sefstring.=$parts['sectionid'].'/';
}
//id
if(isset($parts['id'])){
$sefstring.=$parts['id'].'/';
}
//Itemid
if(isset($parts['Itemid'])){
//onlyaddItemidvalueifitdoesnotcorrespondwiththe'unassigned'Itemidvalue
if($parts['Itemid']!=99999999&&$parts['Itemid']!=0){
$sefstring.=$parts['Itemid'].'/';
}
}
//order
if(isset($parts['order'])){
$sefstring.='order,'.$parts['order'].'/';
}
//filter
if(isset($parts['filter'])){
$sefstring.='filter,'.$parts['filter'].'/';
}
//limit
if(isset($parts['limit'])){
$sefstring.=$parts['limit'].'/';
}
//limitstart
if(isset($parts['limitstart'])){
$sefstring.=$parts['limitstart'].'/';
}
//year
if(isset($parts['year'])){
$sefstring.=$parts['year'].'/';
}
//month
if(isset($parts['month'])){
$sefstring.=$parts['month'].'/';
}
//module
if(isset($parts['module'])){
$sefstring.=$parts['module'].'/';
}
//lang
if(isset($parts['lang'])){
$sefstring.='lang,'.$parts['lang'].'/';
}
$string=$sefstring;
//allothercomponents
//index.php?option=com_xxxx&...
}elseif(isset($parts['option'])&&(strpos($parts['option'],'com_')