1、關(guān)于Apache 2.x URL別名的說(shuō)明和設(shè)置;
Apache 2.x 服務(wù)器中的URL別名規(guī)則的文檔,它是通過(guò)rewrite模塊來(lái)實(shí)現(xiàn)的。能過(guò)URL別名規(guī)則,我們能看到一個(gè)干凈的URL,比如可以重寫為類似靜態(tài)網(wǎng)頁(yè)的地址。比如 LinuxSir.Org 論壇中,每個(gè)帖子都有一個(gè)靜態(tài)網(wǎng)頁(yè)的地址。干凈的URL,對(duì)于Google搜索引擎來(lái)說(shuō)是極為受用的,能更快的收錄。
這個(gè)重寫,是通過(guò)Apache 2.x 內(nèi)部實(shí)現(xiàn)的,只是表面上把Web應(yīng)用程序的URL變的干凈一點(diǎn),原始的URL還是一樣有效。
1.1 關(guān)于rewrite模塊的調(diào)用;
Apache 2.x 中URL重寫,是通過(guò)mod_rewrite.so 來(lái)實(shí)現(xiàn)的,所以您要查看您的Apache 是否已經(jīng)被編譯進(jìn)去這個(gè)模塊了,并且在Apache的配置文件httpd.conf 中已經(jīng)調(diào)用了這個(gè)模塊。在大多數(shù)主流發(fā)行版中,Apache 2.x 是把rewrite模塊已經(jīng)編入進(jìn)去了。比如我用的是Slackware。Apache 2.x的配置文件,放在 /etc/httpd 目錄下。
在 httpd.conf 中,我們會(huì)發(fā)現(xiàn)類似如下的一行,是有關(guān)rewrite模塊的,模塊名是 mod_rewrite.so 。
LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so
或
LoadModule rewrite_module lib/apache2/modules/mod_rewrite.so
如果前面有#號(hào),您要去掉。對(duì)于大多數(shù)發(fā)行版來(lái)說(shuō),Apache 2的模塊一般是位于如下的兩個(gè)位置
/usr/lib/apache2/modules
或
/usr/lib/httpd/modules
如果在httpd中打開調(diào)用rewrite_module的設(shè)置,查看一下是不是能調(diào)用了,要通過(guò) httpd -M的參數(shù)來(lái)查看;
#/usr/sbin/httpd -M
如果發(fā)現(xiàn)有如下一行,說(shuō)明模塊已經(jīng)能被調(diào)用了
rewrite_module (shared)
1.2 設(shè)置DocumentRoot的Directory;
在Apache 2.x 中,我們會(huì)看到 DocumentRoot設(shè)置的一行。這行就是存放網(wǎng)頁(yè)程序的地方。比如LinuxSir.Org 存放在 /opt/www 目錄中。那么我們就要設(shè)置 DocumentRoot為如下的。
DocumentRoot /opt/www
然后我們?cè)龠€要對(duì) DocumentRoot做針對(duì)性的行為設(shè)置。在一般的情況下,httpd.conf 會(huì)給一個(gè)默認(rèn)的。如果你要改 DocumentRoot的路徑,同時(shí)也要改針對(duì)DocumentRoot的Directory的設(shè)置,也就是
Directory DocumentRoot所設(shè)置的路徑
比如我們把DocumentRoot的路徑改為了 /opt/www,那我們也要把 DocumentRoot做針對(duì)性的行為設(shè)置也要改成這個(gè)路徑。
Directory /opt/www
Options FollowSymLinks
#AllowOverride None 注:把這行前面加#號(hào),然后加下面的一行 ,也就是 AllowOverride ALL
AllowOverride ALL
Order allow,deny
Allow from all
/Directory
我們把AllowOverride 的參數(shù)設(shè)置為ALL,表示整臺(tái)服務(wù)器上的,都支持URL規(guī)則重寫。Apache 服務(wù)器要讀每個(gè)網(wǎng)站下的家目錄下的 .htaccess 文件。如果沒有這個(gè)文件,或者這個(gè)文檔沒有定義任何關(guān)于URL重寫的規(guī)則,則不會(huì)有任何效果。在一般的情況下,成熟的Web 服務(wù)器應(yīng)用套件,都支持URL重寫的,比如drupal和joomla 。當(dāng)我們用這些程序時(shí),會(huì)發(fā)現(xiàn)在安裝包中有 .htaccess中有這個(gè)文件。我們把Apache配置好后,只是需要在這些程序的后臺(tái)打開此功能就行了。
1.3 重啟httpd服務(wù)器;
在一般情況下,在各個(gè)發(fā)行版中,都有httpd服務(wù)器啟動(dòng)腳本,比如
# /etc/rc.d/rc.httpd restart 注:Slackware Linux
# /etc/init.d/apache2 restart 注:ubuntu、Debian 等;
# /etc/init.d/httpd start 注:Fedora 、Redhat、CentOS
2、關(guān)于Apache 2.x URL重寫規(guī)則的應(yīng)用;
Apache 2.x 的rewrite模塊調(diào)用和配置比較容易,這并不是目的。在應(yīng)用為王的年代里,在服務(wù)器程序可選擇的今天,應(yīng)用才是王道。現(xiàn)在我就舉例,說(shuō)一下兩個(gè)應(yīng)用。一個(gè)vbb論壇的URL重寫成類似的靜態(tài)網(wǎng)頁(yè)地址的,另一個(gè)就是drupal的URL地址重寫。
關(guān)于vbb論壇程序的URL規(guī)則重寫,我是從國(guó)外論壇看到的。可能有的朋友需要,對(duì)vbb 3.5或以上版本有效。我測(cè)試的是3.6版本,至于在其它版本上能否可行,這個(gè)由你來(lái)測(cè)試。我不敢保證。
2.1 vbb 論壇的URL地址重寫配置
首先:在您的論壇程序存放的家目錄下,創(chuàng)建一個(gè).htaccess 文件;
內(nèi)空如下:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^getdaily.html$ search.php?do=getdaily [L]
RewriteRule ^getdaily([0-9]+).html$ search.php?do=getdailyf=$1 [L]
RewriteRule ^unanswered.html$ search.php?do=processreplyless=1replylimit=0dontcache=1 [L]
RewriteRule ^unanswered([0-9]+).html$ search.php?do=processreplyless=1replylimit=0dontcache=1forumchoice=$1childforums=1 [L]
RewriteRule ^forum([0-9]+).html$ forumdisplay.php?f=$1 [L]
RewriteRule ^forum([0-9]+)-([0-9]+)-([a-z]+)-(.*)-([0-9]+)-(.*).html$ forumdisplay.php?f=$1page=$2sort=$3order=$4pp=$5daysprune=$6 [L]
RewriteRule ^forum([0-9]+)-(.*)-([a-z]+)-([a-z]+).html$ forumdisplay.php?f=$1daysprune=$2order=$3sort=$4 [L]
RewriteRule ^announcement([0-9]+).html$ announcement.php?f=$1 [L]
RewriteRule ^announcement([0-9]+)-([0-9]+).html$ announcement.php?f=$1announcementid=$2 [L]
RewriteRule ^thread([0-9]+).html$ showthread.php?t=$1 [L]
RewriteRule ^thread([0-9]+)-([0-9]+).html$ showthread.php?t=$1page=$2 [L]
RewriteRule ^getnew.html$ search.php?do=getnew [L]
RewriteRule ^getnew([0-9]+).html$ search.php?do=getnewf=$1 [L]
RewriteRule ^printthread([0-9]+).html$ printthread.php?t=$1 [L]
RewriteRule ^sendthread([0-9]+).html$ sendmessage.php?do=sendtofriendt=$1 [L]
RewriteRule ^referthread([0-9]+)-([0-9]+).html$ showthread.php?t=$1referrerid=$2 [L]
RewriteRule ^lastpostinthread([0-9]+).html$ showthread.php?goto=lastpostt=$1 [L]
RewriteRule ^newpostinthread([0-9]+).html$ showthread.php?goto=newpostt=$1 [L]
RewriteRule ^nextnewesttothread([0-9]+).html$ showthread.php?t=$1goto=nextnewest [L]
RewriteRule ^nextoldesttothread([0-9]+).html$ showthread.php?t=$1goto=nextoldest [L]
RewriteRule ^post([0-9]+).html$ showthread.php?p=$1 [L]
RewriteRule ^post([0-9]+)-([0-9]+).html$ showpost.php?p=$1postcount=$2 [L]
RewriteRule ^post([0-9]+)-([0-9]+)-([0-9]+).html$ showthread.php?p=$1page=$2pp=$3 [L]
RewriteRule ^thread([0-9]+)-([a-z]+).html$ showthread.php?mode=$2t=$1 [L]
RewriteRule ^post([0-9]+)-([a-z]+).html$ showthread.php?p=$1mode=$2 [L]
第二:創(chuàng)建一個(gè)xml文件;
比如您可以用 vbburlhack.xml ,在這個(gè)文件內(nèi)貼上如下內(nèi)容;
?xml version=1.0 encoding=UTF-8?
product productid=danis_seo_optimization active=1
titleDanis Seo Optimization/title
description![CDATA[This is a very quick n simple vBulletin SEO hack. Its easy to apply and its the one I have been using here on DaniWeb for the past couple of months. I was going to wait until we went vB 3.5 to release it but I might as well do so now ... Disclaim]]/description
version1.0/version
codes
/codes
templates
/templates
plugins
plugin active=1
title![CDATA[Danis SEO optimization]]/title
hooknameglobal_complete/hookname
phpcode![CDATA[// do Danis SEO optimization
global $session;
$search_array = array(
#a ([^]*)href . preg_quote(=forumdisplay.php?$session[sessionurl]f=) . ([0-9]+) . preg_quote(amp;page=) . ([0-9]+) . preg_quote(amp;sort=) . ([a-z]*) . preg_quote(amp;order=). ([a-z]*) . preg_quote(amp;pp=) . ([0-9]*) . preg_quote(amp;daysprune=). ([^]*)#,
#a ([^]*)href . preg_quote(=forumdisplay.php?$session[sessionurl]f=) . ([0-9]+) . preg_quote(amp;daysprune=) . ([^]*) . preg_quote(amp;order=) . ([a-z]*) . preg_quote(amp;sort=) . ([a-z]*) . preg_quote(amp;pp=) . ([0-9]*) . preg_quote(amp;page=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=forumdisplay.php?$session[sessionurl]f=) . ([0-9]+) . preg_quote(amp;daysprune=) . ([^^]*) . preg_quote(amp;order=) . ([a-z]*) . preg_quote(amp;sort=) . ([a-z]*)#,
#a ([^]*)href . preg_quote(=forumdisplay.php?$session[sessionurl]f=) . ([0-9]+) . preg_quote(amp;daysprune=). ([^^]*)#,
#a ([^]*)href . preg_quote(=forumdisplay.php?$session[sessionurl]f=) . ([0-9]+) . preg_quote(amp;page=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=forumdisplay.php?$session[sessionurl]f=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]t=) . ([0-9]+) . preg_quote(amp;page=) . ([0-9]*) . preg_quote(amp;pp=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]t=) . ([0-9]+) . preg_quote(amp;page=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]goto=lastpostamp;t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]goto=newpostamp;t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=printthread.php?$session[sessionurl]t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=sendmessage.php?$session[sessionurl]do=sendtofriendamp;t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]t=) . ([0-9]+) . preg_quote(amp;goto=next). ([a-z]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]p=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]p=) . ([0-9]+) . preg_quote(amp;page=) . ([0-9]+) . preg_quote(amp;pp=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showpost.php?$session[sessionurl]p=) . ([0-9]+) . preg_quote(amp;postcount=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]mode=) . ([a-z]+) . preg_quote(amp;t=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=showthread.php?$session[sessionurl]p=) . ([0-9]+) . preg_quote(amp;mode=) . ([a-z]+)##,
#a ([^]*)href . preg_quote(=announcement.php?$session[sessionurl]f=) . ([0-9]+) . preg_quote(amp;announcementid=) . ([0-9]+)#,
#a ([^]*)href . preg_quote(=announcement.php?$session[sessionurl]f=) . ([0-9]+)#,
// sanitizing
#a ([^]*)href=([^]*)amp;page=([^]*).html#,
#a ([^]*)href=([^]*)amp;highlight=([^]*).html#,
// other
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=getdailyamp;f=) . ([0-9]*)#,
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=getdaily) . #,
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=processamp;replyless=1amp;replylimit=0amp;dontcache=1amp;forumchoice=amp;childforums=1) . #,
#a ([^]*)href . preg_quote(=search.php?$session[sessionurl]do=processamp;replyless=1amp;replylimit=0amp;dontcache=1amp;forumchoice=) . ([0-9]+) . preg_quote(amp;childforums=1) . #
);
$replace_array = array(
a 1href=forum2-3-4-5-6-7.html,
a 1href=forum2-7-5-4-6-3.html,
a 1href=forum2-3-4-5.html,
a 1href=forum2-3.html,
a 1href=forum2-3.html,
a 1href=forum2.html,
a 1href=thread2-3.html,
a 1href=thread2-3.html,
a 1href=thread2.html,
a 1href=lastpostinthread2.html,
a 1href=newpostinthread2.html,
a 1href=printthread2.html,
a 1href=sendthread2.html,
a 1href=next3tothread2.html,
a 1href=post2.html,
a 1href=post2-3-4.html,
a 1href=post2-3.html,
a 1href=thread3-2.html,
a 1href=post2-3.html#,
a 1href=announcement2-3.html,
a 1href=announcement2.html,
// sanitizing
a 1href=2-3.html,
a 1href=2-3.html,
// other
a 1href=getdaily2.html,
a 1href=getdaily.html,
a 1href=unanswered.html,
a 1href=unanswered2.html
);
$output = preg_replace($search_array, $replace_array, $output);]]/phpcode
/plugin
/plugins
phrases
/phrases
options
/options
/product
第三:從vbb后臺(tái)登錄,點(diǎn)擊產(chǎn)品,然后導(dǎo)入這個(gè)vbburlhack.xml 文件;
只要導(dǎo)入,就能成功了。效果可參見 http://www.linuxsir.org/bbs
注意:如果出現(xiàn)錯(cuò)誤,就是你的.htaccess 沒放對(duì)地方,或者權(quán)限不對(duì)。如果有問(wèn)題,不要來(lái)問(wèn)我,這個(gè)插件不是我寫的,是洋人寫的,我只是搬過(guò)來(lái)應(yīng)用。呵。。。。出問(wèn)題就去找他們吧。謝謝了~~~~
2.2 drupal 的URL地址重寫設(shè)置;
drupal就比較簡(jiǎn)單了,不象vbb一樣,要自己來(lái)弄,drupal程序本身就提供了.htaccess 文件。在安裝程序的目錄里就有。我們只是設(shè)置一下就能用了。要通過(guò)管理員帳號(hào)登錄。如果您安裝的了中文模塊的支持,就是定義干凈的URL,他要我們來(lái)測(cè)試是否能用。如果測(cè)試通過(guò),打開就是了。只是點(diǎn)鼠標(biāo)。如果你沒有通過(guò),說(shuō)明你把.htaccess 文件給丟了,下載一份drupal,把這個(gè)文件復(fù)制過(guò)去就行了。注意是.htaccess ,前面有個(gè)小點(diǎn)
3、關(guān)于本文;
本文是在維護(hù) LinuxSir.Org 服務(wù)器的過(guò)程中,想到Clean URL是比較重要的, 因?yàn)檫@樣Google就可以更加有效率的收錄網(wǎng)頁(yè)了。我們可以利用Google的搜索找到我們所需要的東西。在一定程度上,緩解了 LinuxSir.Org 服務(wù)器的壓力。我們把搜索、郵局讓Google來(lái)托管,可以減輕我們維護(hù)服務(wù)器的工作量,感謝Google!