程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP綜合 >> php 友好URL的實現(吐血推薦)

php 友好URL的實現(吐血推薦)

編輯:PHP綜合
友好URL的實現(吐血推薦)
大家經常看到別的站的URL是這樣的吧?
http://www.xxx.com/module/show/action/list/page/7
或者
http://xx.com/module/show/action/show/id/8.shtml 帶擴展名的
或者
http://xx.com/module/show/action/show/id/8?word=ss&age=11
這樣的吧
今天我就是公布下這種方法的實現,並獨立出最簡單的代碼
函數如下,沒封裝成類,主要是沒必要,用函數能方便些
復制代碼 代碼如下:
<?php
/**
* 獲得友好的URL訪問
*
* @access public
* @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
*
* @access public
* @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;
}
?>

使用很簡單
復制代碼 代碼如下:
<?php
$_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了,趕快去測試吧
復制代碼 代碼如下:
<?php
$_GET= getQueryString();
print_r($_GET);
?>
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved