PHP友好URL的實現_PHP教程

來源:互聯網
上載者:User

下面的代碼主要是偽靜態實現,搜尋引擎喜歡

大家經常看到別的站的URL是這樣的吧?
http://www.xxx.com/module/show/action/list/page/7
或者
html">http://xx.com/module/show/action/show/id/8.shtml 帶副檔名的
或者
http://xx.com/module/show/action/show/id/8?word=ss&age=11
這樣的吧
今天我就是公布下這種方法的實現,並獨立出最簡單的代碼
函數如下,沒封裝成類,主要是沒必要,用函數能方便些

/**
* 獲得友好的URL訪問
*
* @accesspublic
* @return array
*/
function getQueryString(){
$_SGETS = explode("/",substr($_SERVER[PATH_INFO],1));
$_SLEN = count($_SGETS);
$_SGET = $_GET;
for($i=0;$i<$_SLEN;$i+=2){
if(!empty($_SGETS[$i]) && !empty($_SGETS[$i+1])) $_SGET[$_SGETS[$i]]=$_SGETS[$i+1];
}
$_SGET[m] = !empty($_SGET[m]) && is_string($_SGET[m]) ? trim($_SGET[m]).Action : indexAction;
$_SGET[a] = !empty($_SGET[a]) && is_string($_SGET[a]) ? trim($_SGET[a]) : run;
return $_SGET;
}
/**
* 產生連結URL
*
* @accesspublic
* @param array $arr
* @return string
*/
function setUrl($arr){
global $Global;
$queryString=;
if($Global[urlmode]==2){
foreach($arr as $k=> $v){
$queryString.=$k./.$v./;
}
}
$queryString.=$Global[urlsuffix];
return $queryString;
}
?>
使用很簡單
$_GET= getQueryString();
?>
但是這樣還不行,這樣只能實現
http://www.xxx.com/index.php/module/show/action/list/page/7 這樣的
中間多了個index.php 為此我們要把他去掉,只好重寫
但是有些檔案 又不希望這樣,比如 樣式 圖片,那就放條件裡
建立一個 .htaccess檔案

RewriteEngine on
RewriteCond $1 !^(index.php|css|pics|themes|js|robots.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
現在OK了,趕快去測試吧

$_GET= getQueryString();
print_r($_GET);
?>

http://www.bkjia.com/PHPjc/508354.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/508354.htmlTechArticle下面的代碼主要是偽靜態實現,搜尋引擎喜歡 大家經常看到別的站的URL是這樣的吧? http://www.xxx.com/module/show/action/list/page/7 或者 html">h...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.