Joomla中的SEF

Joomla是目前比较盛行的CMS系统,良好的框架结构使越来越多的开发人员加入进来。使用Joomla有一段时间的人都知道Joomla所生成的站点一般都是基于动态URL的对于搜索引擎来说动态URL的站点并不是就不能搜索到就一定会排在后面而是不太友好,这个不友好也是暂时的目前的个别现象,事物都是进步的GOOGLE也会在不断进步,其实我并不喜欢那些把搜索引擎吹呼的不得了的人,这个优化大师那个优化姥姥的,那些人八成都是有一点点个人的私欲在里面,要是不小心刺激到你,抱歉我不是有意在说你!

Joomla中的SEF说白了就是一个对URL的重写的过程将原来参数众多,层次很深的URL改写为一个简单的更容易被记住被搜索的URL。通过分析Joomla站点的URL结果就会发现规律很明显:

                  域名+index.php?option=com_content&task=category&sectionid=4&id=13&Itemid=27

以上就是一个最普通不过的URL,其中包含的元素有option(组件参数,告诉系统一下内容来自哪个组件)、task(任务参数,组件内执行什么任务上面的例子中代表执行分类列表,sectionid内容的单元号JOOMLA特有,id,itemid项目号)。Joomla本身就自带一个URL优化的组件,也就是一个函数实现对上述地址的重写为index.php/content/view/4/13/27.html,是不是貌似静态,严格来说应该是伪装的静态。

下面的函数sefRelToAbs就是实现上述改写的

/* *
 * Converts an absolute URL to SEF format
 * @param string The URL
 * @return string
 
*/
function  sefRelToAbs(  $string  ) {
    
global   $mosConfig_live_site ,   $mosConfig_sef ,   $mosConfig_mbf_content ,   $mosConfig_multilingual_support ;
    
global   $iso_client_lang ;

    
// multilingual code url support
     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 ;
    }

    
//  SEF URL Handling
     if  ( $mosConfig_sef   &&   ! eregi ( " ^(([^:/?#]+):) " , $string &&   ! strcasecmp ( substr ( $string , 0 , 9 ) , ' index.php ' )) {
        
//  Replace all & with &
         $string   =   str_replace ' & ' ,   ' & ' ,   $string  );

        
//  Home index.php
         if  ( $string   ==   ' index.php ' ) {
            
$string   =   '' ;
        }
        
        
//  break link into url component parts
         $url   =   parse_url $string  );

        
//  check if link contained fragment identifiers (ex. #foo)
         $fragment   =   '' ;
        
if  (  isset ( $url [ ' fragment ' ]) ) {
            
//  ensure fragment identifiers are compatible with HTML4
             if  ( preg_match ( ' @^[A-Za-z][A-Za-z0-9:_.-]*$@ ' ,   $url [ ' fragment ' ])) {
                
$fragment   =   ' # ' .   $url [ ' fragment ' ];
            }
        }

        
//  check if link contained a query component
         if  (  isset ( $url [ ' query ' ]) ) {
            
//  special handling for javascript
             $url [ ' query ' =   stripslashes str_replace ' + ' ,   ' %2b ' ,   $url [ ' query ' ] ) );
            
//  clean possible xss attacks
             $url [ ' query ' =   preg_replace " '%3Cscript[^%3E]*%3E.*?%3C/script%3E'si " ,   '' ,   $url [ ' query ' ] );

            
//  break url into component parts            
             parse_str $url [ ' query ' ] ,   $parts  );

            
//  special handling for javascript
             foreach $parts   as   $key   =>   $value ) {
                
if  (  strpos $value ,   ' + '  )  !==   false  ) {
                    
$parts [ $key =   stripslashes str_replace ' %2b ' ,   ' + ' ,   $value  ) );
                }
            }
            
// var_dump($parts);
             $sefstring   =   '' ;

            
//  Component com_content urls
             if  ( ( (  isset ( $parts [ ' option ' ])  &&  (  $parts [ ' option ' ==   ' com_content '   ||   $parts [ ' option ' ==   ' content '  ) ) )  &&  (  $parts [ ' task ' !=   ' new '  )  &&  (  $parts [ ' task ' !=   ' edit '  ) ) {
            
//  index.php?option=com_content [&task=$task] [&sectionid=$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 ' ] ) ) {
                    
// only add Itemid value if it does not correspond with the 'unassigned' Itemid value
                     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 ;
                
            
//  all other components
            // index.php?option=com_xxxx &...

            }  else   if  (  isset ( $parts [ ' option ' ])  &&  (  strpos ( $parts [ ' option ' ] ,   ' com_ '  )  !==   false  ) ) {
                
//  do not SEF where com_content - `edit` or `new` task link                
                 if  (  ! ( (  $parts [ ' option ' ==   ' com_content '  )  &&  ( (  isset ( $parts [ ' task ' ])  ==   ' new '  )  ||  (  isset ( $parts [ ' task ' ])  ==   ' edit '  ) ) ) ) {
                    
$sefstring       =   ' component/ ' ;
                    
                    
foreach ( $parts   as   $key   =>   $value ) {
                        
//  remove slashes automatically added by parse_str
                         $value          =   stripslashes ( $value );
                        
$sefstring   .=   $key   . ' , ' .   $value . ' / ' ;
                    }
                    
                    
$string   =   str_replace ' = ' ,   ' , ' ,   $sefstring  );
                }
            }
        }

        
//  allows SEF without mod_rewrite
        // comment line below if you dont have mod_rewrite

         return   $mosConfig_live_site   . ' / ' .   $string   .   $fragment ;

        
//  allows SEF without mod_rewrite
        // uncomment Line 512 and comment out Line 514    
    
        // uncomment line below if you dont have mod_rewrite
        // return $mosConfig_live_site .'/index.php/'. $string . $fragment;
        // If the above doesnt work - try uncommenting this line instead
        // return $mosConfig_live_site .'/index.php?/'. $string . $fragment;

    }  else  {
    
//  Handling for when SEF is not activated
        // Relative link handling

         if  ( ( strpos $string ,   $mosConfig_live_site  )  !==   0 ) ) {
            
//  if URI starts with a "/", means URL is at the root of the host...
             if  ( strncmp ( $string ,   ' / ' ,   1 ==   0 ) {
                
//  splits http(s)://xx.xx/yy/zz..." into [1]="http(s)://xx.xx" and [2]="/yy/zz...":
                 $live_site_parts   =   array ();
                
eregi ( " ^(https?:[/]+[^/]+)(.*$) " ,   $mosConfig_live_site ,   $live_site_parts );
                
                
$string   =   $live_site_parts [ 1 .   $string ;
            } 
else  {
                
$check   =   1 ;
                
                
//  array list of non http/https    URL schemes
                 $url_schemes       =   explode ' ' ,  _URL_SCHEMES );
                
$url_schemes []      =   ' http: ' ;
                
$url_schemes []      =   ' https: ' ;

                
foreach  (  $url_schemes   as   $url  ) {
                    
if  (  strpos $string ,   $url  )  ===   0  ) {
                        
$check   =   0 ;
                    }
                }
                
                
if  (  $check  ) {
                    
$string   =   $mosConfig_live_site   . ' / ' .   $string ;
                }
            }
        }
        
        
return   $string ;
    }
}

要想使用这个函数首先要确定你的服务器是否支持URL重写,一般来说只有APACHE服务器支持,IIS就不要打算了。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值