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'= stripslashesstr_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= stripslashesstr_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就不要打算了。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章