- 相關(guān)推薦
php的apache偽靜態(tài)
導(dǎo)語:現(xiàn)有的在線網(wǎng)上視頻教程對(duì)偽靜態(tài)的講解比較簡(jiǎn)單,但不全面,小編以一個(gè)真實(shí)案例來講解偽靜態(tài)的制作過程。歡迎參考!
步驟開始:
(1) 啟用rewrite模塊,在默認(rèn)情況下,沒有啟用
修改httpd.conf文件,啟動(dòng)rewrite模塊
去掉LoadModule rewrite_module modules/mod_rewrite.so前的#號(hào)即可
(2) 配置我們的虛擬主機(jī)
httpd.conf 打開虛擬主機(jī)的配置文件
# Virtual hosts
Include conf/extra/httpd-vhosts.conf
修改 httpd-vhost.conf
<VirtualHost *:80>
DocumentRoot "F:/Appserv/www/xh"
ServerName xh.com
<Directory "F:/Appserv/www/xh">
AllowOverride All
</Directory>
</VirtualHost>
我是用的是appserv集成環(huán)境,安裝在F盤
(3) 在hosts文件中,配置ip和主機(jī)的對(duì)應(yīng)關(guān)系
127.0.0.1 xh.com
(4) 在F:/Appserv/www/xh目錄下建立.htaccess文件,寫入
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^([0-9]+).html$ index.php/Index/index/p/$1
RewriteRule ^([A-Z])_(d+).html$ index.php/List/index/first_letter/$1/p/$2
RewriteRule ^([A-Z]).html$ index.php/List/index/first_letter/$1
</IfModule>
解釋一下上面那段話,
訪問2.html => index.php/Index/index/p/2
D_2.html => index.php/List/index/first_letter/D/p/2
D.html => index.php/List/index/first_letter/D
2.html表示全部歇后語的第二頁,D_2.html表示以字母D打頭的歇后語的第二頁,而單獨(dú)一個(gè)字母D就表示以D打頭的以第一頁
好了問題來了,大部分教程只告訴你怎么在.htaccess中重寫url,那么我們要讓用戶點(diǎn)擊時(shí)顯示的也是靜態(tài)網(wǎng)址,這樣表意清晰,目錄結(jié)構(gòu)簡(jiǎn)單,對(duì)用戶對(duì)搜索引擎都比較友好,我們是不會(huì)在地址欄里頭一個(gè)一個(gè)的敲入靜態(tài)網(wǎng)址的,這個(gè)問題該怎么解決呢?
很簡(jiǎn)單,只需對(duì)模板中的分頁標(biāo)簽變量{$page}做一個(gè)簡(jiǎn)單的正則替換,如下,
首頁列表分頁的替換:
<div class="pagination"><?php echo preg_replace('/index.php/Index/index/p/(d+).html/','$1.html',$page); ?></div>
字母列表分頁的替換:<div class="pagination"><?php echo preg_replace('/index.php/List/index/first_letter/([A-Z])/p/(d+).html/','$1_$2.html',$page); ?></div>
循環(huán)26個(gè)字母的改寫(去掉沒有結(jié)果的那些字母,只需做一個(gè)簡(jiǎn)單的鏈接改寫,改成 字母.html 即可,無需正則替換)
for($i=97;$i<=122;$i++) {
$c = strtoupper(chr($i));
if($c==I || $c==U || $c==V) continue;
echo '<li><a href="' . $c . '.html">'.$c.'</a></li>';
}
好了,偽靜態(tài)就這么簡(jiǎn)單,我以這個(gè)簡(jiǎn)單的例子闡述了偽靜態(tài)從頭到尾的過程,方便大家學(xué)習(xí)和交流,目的在于針對(duì)多數(shù)教程的一個(gè)補(bǔ)充,需要完成更復(fù)雜任務(wù)的同學(xué),請(qǐng)自行深入研究偽靜態(tài)吧!
【php的apache偽靜態(tài)】相關(guān)文章:
PHP偽靜態(tài)的方法10-26
php簡(jiǎn)單偽靜態(tài)實(shí)例09-16
PHP偽靜態(tài)的幾種方法06-01
php實(shí)現(xiàn)偽靜態(tài)的方法實(shí)例09-25
PHP簡(jiǎn)單的偽靜態(tài)URL機(jī)制實(shí)現(xiàn)09-02
php靜態(tài)成員變量06-22