比如,下面,我要實現(xiàn)這樣的URL:
http://xxx.com/0725
http://xxx.com/0726
http://xxx.com/0727
…
上面xxx.com是一個記錄《今天是》這樣的歷史內容的一個測試站點。上面鏈接的意思,就是顯示07月25日的歷史上發(fā)生了什么事情,類似這樣。這樣看起來就很美觀、整齊了。要不然,可能的地址就是:
http://xxx.com/index.php?t...
http://xxx.com/index.php?t...
http://xxx.com/index.php?t...
…
現(xiàn)在我就是要實現(xiàn)把 index.php?today= 隱藏掉。以下是代碼:
1..htaccess 文件
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /index.php?today=$1
</IfModule>
粗體字這里我說明一下,其它的按這個格式吧,具體我現(xiàn)在也不理解。
[0-9]的意思是,參數(shù)只能是0~9這些數(shù)字,如果你要包含任何字符,就改為:
RewriteRule ^(.+)$ /index.php?today=$1
這里[0-9]改為了. ,這個.就代表任意字符。當然復雜的還很復雜,我們暫時不管。
2.index.php文件
復制代碼 代碼如下:
<?php
//rewrite 測試
$uid =$_REQUEST['today'];
?>
<HTML>
<HEAD>
<TITLE>rewrite 測試</TITLE>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
今天是<?php echo $today;?>,看看歷史上的今天都發(fā)生了什么事情?<br>
……
</BODY>
</HTML>
這里參數(shù)就會傳遞給index.php文件里的$today,在這個程序內,你就可以根據(jù)參數(shù),進行處理,如查詢數(shù)據(jù)庫啦、做運算啦等,然后再顯示相應的數(shù)據(jù)出來,就可以了。
實現(xiàn)方法2
復制代碼 代碼如下:
<?php
//url示例:soft.php/1,100,8630.html
//利用server變量 取得PATH_INFO信息 該例中為 /1,100,8630.html 也就是執(zhí)行腳本名后面的部分
if(@$path_info =$_SERVER["PATH_INFO"]){
if(preg_match("/\/(\d+),(\d+),(\d+)\.html/si",$path_info,$arr_path)){
$gid =intval($arr_path[1]); //取得值 1
$sid =intval($arr_path[2]); //取得值100
$softid =intval($arr_path[3]); //取得值8630
//相當于soft.php?gid=1&sid=100&softid=8630
}else die("Path:Error!");
}else die("Path:Nothing!");
echo($gid);
echo("<br>");
echo($sid);
echo("<br>");
echo($softid);
?>