天天看點

Apache 2.x 伺服器中的URL重寫的配置和應用(轉)

作者: 北南南北

來自: LinuxSir.Org

摘要: 本文是關于Apache 2.x 伺服器中的URL别名規則的文檔,它是通過rewrite子產品來實作的。能過URL别名規則,我們能看到一個幹淨的URL,比如可以重寫為類似靜态網頁的 位址。比如 LinuxSir.Org 論壇中,每個文章都有一個靜态網頁的位址。幹淨的URL,對于Google搜尋引擎來說是極為受用的,能更快的收錄。本文隻是一個小提示,或者對有些弟兄 有用。

目錄

    •   1.1 關于rewrite子產品的調用;

      1.2 設定DocumentRoot的Directory;

      1.3 重新開機httpd伺服器;

    2.1 vbb 論壇的URL位址重寫配置

    2.2 drupal 的URL位址重寫設定;

1、關于Apache 2.x URL别名的說明和設定;

2、關于Apache 2.x URL重寫規則的應用;

3、關于本文;

4、修改日志;

5、參考文檔;

6、相關文檔;

++++++++++++++++++++++++++++++++++++

正文

++++++++++++++++++++++++++++++++++++

1、關于Apache 2.x URL别名的說明和設定;

Apache 2.x 伺服器中的URL别名規則的文檔,它是通過rewrite子產品來實作的。能過URL别名規則,我們能看到一個幹淨的URL,比如可以重寫為類似靜态網頁的 位址。比如 LinuxSir.Org 論壇中,每個文章都有一個靜态網頁的位址。幹淨的URL,對于Google搜尋引擎來說是極為受用的,能更快的收錄。

這個重寫,是通過Apache 2.x 内部實作的,隻是表面上把Web應用程式的URL變的幹淨一點,原始的URL還是一樣有效。

1.1 關于rewrite子產品的調用;

Apache 2.x 中URL重寫,是通過mod_rewrite.so 來實作的,是以您要檢視您的Apache 是否已經被編譯進去這個子產品了,并且在Apache的配置檔案httpd.conf 中已經調用了這個子產品。在大多數主流發行版中,Apache 2.x 是把rewrite子產品已經編入進去了。比如我用的是Slackware。Apache 2.x的配置檔案,放在 /etc/httpd 目錄下。

在 httpd.conf 中,我們會發現類似如下的一行,是有關rewrite子產品的,子產品名是 mod_rewrite.so 。

LoadModule rewrite_module lib/httpd/modules/mod_rewrite.so 或 LoadModule rewrite_module lib/apache2/modules/mod_rewrite.so

如果前面有#号,您要去掉。對于大多數發行版來說,Apache 2的子產品一般是位于如下的兩個位置

/usr/lib/apache2/modules 或 /usr/lib/httpd/modules

如果在httpd中打開調用rewrite_module的設定,檢視一下是不是能調用了,要通過 httpd -M的參數來檢視;

#/usr/sbin/httpd -M

如果發現有如下一行,說明子產品已經能被調用了

rewrite_module (shared)

1.2 設定DocumentRoot的Directory;

在Apache 2.x 中,我們會看到 DocumentRoot設定的一行。這行就是存放網頁程式的地方。比如LinuxSir.Org 存放在 /opt/www 目錄中。那麼我們就要設定 DocumentRoot為如下的。

DocumentRoot "/opt/www"

然後我們再還要對 DocumentRoot做針對性的行為設定。在一般的情況下,httpd.conf 會給一個預設的。如果你要改 DocumentRoot的路徑,同時也要改針對DocumentRoot的Directory的設定,也就是

<Directory "DocumentRoot所設定的路徑">

比如我們把DocumentRoot的路徑改為了 "/opt/www",那我們也要把 DocumentRoot做針對性的行為設定也要改成這個路徑。

<Directory "/opt/www">     Options FollowSymLinks     #AllowOverride None 注:把這行前面加#号,然後加下面的一行  ,也就是   AllowOverride ALL     AllowOverride ALL        Order allow,deny     Allow from all </Directory>

我們把AllowOverride 的參數設定為ALL,表示整台伺服器上的,都支援URL規則重寫。Apache 伺服器要讀每個網站下的家目錄下的 .htaccess 檔案。如果沒有這個檔案,或者這個文檔沒有定義任何關于URL重寫的規則,則不會有任何效果。在一般的情況下,成熟的Web 伺服器應用套件,都支援URL重寫的,比如drupal和joomla 。當我們用這些程式時,會發現在安裝包中有 .htaccess中有這個檔案。我們把Apache配置好後,隻是需要在這些程式的背景打開此功能就行了。

1.3 重新開機httpd伺服器;

在一般情況下,在各個發行版中,都有httpd伺服器啟動腳本,比如

# /etc/rc.d/rc.httpd restart   注:Slackware Linux # /etc/init.d/apache2 restart  注:ubuntu、Debian 等; # /etc/init.d/httpd start  注:Fedora 、Redhat、CentOS

2、關于Apache 2.x URL重寫規則的應用;

Apache 2.x 的rewrite子產品調用和配置比較容易,這并不是目的。在應用為王的年代裡,在伺服器程式可選擇的今天,應用才是王道。現在我就舉例,說一下兩個應用。一個vbb論壇的URL重寫成類似的靜态網頁位址的,另一個就是drupal的URL位址重寫。

關于vbb論壇程式的URL規則重寫,我是從國外論壇看到的。可能有的朋友需要,對vbb 3.5或以上版本有效。我測試的是3.6版本,至于在其它版本上能否可行,這個由你來測試。我不敢保證。

2.1 vbb 論壇的URL位址重寫配置

首先:在您的論壇程式存放的家目錄下,建立一個.htaccess 檔案;

内空如下:

Options +FollowSymLinks RewriteEngine on RewriteRule ^getdaily.html$ search.php?do=getdaily [L] RewriteRule ^getdaily([0-9]+).html$ search.php?do=getdaily&f=$1 [L] RewriteRule ^unanswered.html$ search.php?do=process&replyless=1&replylimit=0&dontcache=1     [L] RewriteRule ^unanswered([0-9]+).html$ search.php?do=process&replyless=1&replylimit=0&dontcache=1&forumchoice=$1&childforums=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=$1&page=$2&sort=$3&order=$4&pp=$5&daysprune=$6 [L] RewriteRule ^forum([0-9]+)-(.*)-([a-z]+)-([a-z]+).html$    forumdisplay.php?f=$1&daysprune=$2&order=$3&sort=$4     [L] RewriteRule ^announcement([0-9]+).html$     announcement.php?f=$1     [L] RewriteRule ^announcement([0-9]+)-([0-9]+).html$    announcement.php?f=$1&announcementid=$2 [L] RewriteRule ^thread([0-9]+).html$     showthread.php?t=$1     [L] RewriteRule ^thread([0-9]+)-([0-9]+).html$    showthread.php?t=$1&page=$2     [L] RewriteRule ^getnew.html$ search.php?do=getnew [L] RewriteRule ^getnew([0-9]+).html$ search.php?do=getnew&f=$1 [L] RewriteRule ^printthread([0-9]+).html$    printthread.php?t=$1     [L] RewriteRule ^sendthread([0-9]+).html$    sendmessage.php?do=sendtofriend&t=$1 [L] RewriteRule ^referthread([0-9]+)-([0-9]+).html$ showthread.php?t=$1&referrerid=$2    [L] RewriteRule ^lastpostinthread([0-9]+).html$ showthread.php?goto=lastpost&t=$1 [L] RewriteRule ^newpostinthread([0-9]+).html$ showthread.php?goto=newpost&t=$1 [L] RewriteRule ^nextnewesttothread([0-9]+).html$    showthread.php?t=$1&goto=nextnewest    [L] RewriteRule ^nextoldesttothread([0-9]+).html$    showthread.php?t=$1&goto=nextoldest    [L] RewriteRule ^post([0-9]+).html$     showthread.php?p=$1     [L] RewriteRule ^post([0-9]+)-([0-9]+).html$    showpost.php?p=$1&postcount=$2    [L] RewriteRule ^post([0-9]+)-([0-9]+)-([0-9]+).html$ showthread.php?p=$1&page=$2&pp=$3    [L] RewriteRule ^thread([0-9]+)-([a-z]+).html$ showthread.php?mode=$2&t=$1    [L] RewriteRule ^post([0-9]+)-([a-z]+).html$ showthread.php?p=$1&mode=$2    [L]

第二:建立一個xml檔案;

比如您可以用 vbburlhack.xml ,在這個檔案内貼上如下内容;

<?xml version="1.0" encoding="UTF-8"?> <product productid="danis_seo_optimization" active="1"> <title>Danis Seo Optimization</title> <description><![CDATA[This is a very quick 'n' simple vBulletin SEO hack. It's easy to apply and it's 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> <version>1.0</version> <codes> </codes> <templates> </templates> <plugins> <plugin active="1"> <title><![CDATA[Dani's SEO optimization]]></title> <hookname>global_complete</hookname> <phpcode><![CDATA[// do Dani's 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=lastpost&amp;t=") . '([0-9]+)"#', '#<a ([^>]*)href' . preg_quote("=\"showthread.php?$session[sessionurl]goto=newpost&amp;t=") . '([0-9]+)"#', '#<a ([^>]*)href' . preg_quote("=\"printthread.php?$session[sessionurl]t=") . '([0-9]+)"#', '#<a ([^>]*)href' . preg_quote("=\"sendmessage.php?$session[sessionurl]do=sendtofriend&amp;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=getdaily&amp;f=") . '([0-9]*)"#', '#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=getdaily") . '"#', '#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=process&amp;replyless=1&amp;replylimit=0&amp;dontcache=1&amp;forumchoice=&amp;childforums=1") . '"#', '#<a ([^>]*)href' . preg_quote("=\"search.php?$session[sessionurl]do=process&amp;replyless=1&amp;replylimit=0&amp;dontcache=1&amp;forumchoice=") . '([0-9]+)' . preg_quote("&amp;childforums=1") . '"#' ); $replace_array = array( '<a \1href="forum\2-\3-\4-\5-\6-\7.html" target="_blank" rel="external nofollow" ', '<a \1href="forum\2-\7-\5-\4-\6-\3.html" target="_blank" rel="external nofollow" ', '<a \1href="forum\2-\3-\4-\5.html" target="_blank" rel="external nofollow" ', '<a \1href="forum\2-\3.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ', '<a \1href="forum\2-\3.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ', '<a \1href="forum\2.html" target="_blank" rel="external nofollow" ', '<a \1href="thread\2-\3.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ', '<a \1href="thread\2-\3.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ', '<a \1href="thread\2.html" target="_blank" rel="external nofollow" ', '<a \1href="lastpostinthread\2.html" target="_blank" rel="external nofollow" ', '<a \1href="newpostinthread\2.html" target="_blank" rel="external nofollow" ', '<a \1href="printthread\2.html" target="_blank" rel="external nofollow" ', '<a \1href="sendthread\2.html" target="_blank" rel="external nofollow" ', '<a \1href="next\3tothread\2.html" target="_blank" rel="external nofollow" ', '<a \1href="post\2.html" target="_blank" rel="external nofollow" ', '<a \1href="post\2-\3-\4.html" target="_blank" rel="external nofollow" ', '<a \1href="post\2-\3.html" target="_blank" rel="external nofollow" ', '<a \1href="thread\3-\2.html" target="_blank" rel="external nofollow" ', '<a \1href="post\2-\3.html#', '<a \1href=" target="_blank" rel="external nofollow" announcement\2-\3.html"', '<a \1href="announcement\2.html" target="_blank" rel="external nofollow" ', // sanitizing '<a \1href="\2-\3.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ', '<a \1href="\2-\3.html" target="_blank" rel="external nofollow" target="_blank" rel="external nofollow" ', // other '<a \1href="getdaily\2.html" target="_blank" rel="external nofollow" ', '<a \1href="getdaily.html" target="_blank" rel="external nofollow" ', '<a \1href="unanswered.html" target="_blank" rel="external nofollow" ', '<a \1href="unanswered\2.html" target="_blank" rel="external nofollow" ' ); $output = preg_replace($search_array, $replace_array, $output);]]></phpcode> </plugin> </plugins> <phrases> </phrases> <options> </options> </product>

第三:從vbb背景登入,點選産品,然後導入這個vbburlhack.xml 檔案;

隻要導入,就能成功了。效果可參見 http://www.linuxsir.org/bbs

注意: 如果出現錯誤,就是你的.htaccess 沒放對地方,或者權限不對。如果有問題,不要來問我,這個插件不是我寫的,是洋人寫的,我隻是搬過來應用。呵。。。。出問題就去找他們吧。謝謝了~~~~

2.2 drupal 的URL位址重寫設定;

drupal就比較簡單了,不象vbb一樣,要自己來弄,drupal程式本身就提供了.htaccess 檔案。在安裝程式的目錄裡就有。我們隻是設定一下就能用了。要通過管理者帳号登入。如果您安裝的了中文子產品的支援,就是定義幹淨的URL,他要我們來測試 是否能用。如果測試通過,打開就是了。隻是點滑鼠。如果你沒有通過,說明你把.htaccess 檔案給丢了,下載下傳一份drupal,把這個檔案複制過去就行了。注意是.htaccess ,前面有個小點……

3、關于本文;

本文是在維護 LinuxSir.Org 伺服器的過程中,想到Clean URL是比較重要的, 因為這樣Google就可以更加有效率的收錄網頁了。我們可以利用Google的搜尋找到我們所需要的東西。在一定程度上,緩解了 LinuxSir.Org 伺服器的壓力。我們把搜尋、郵局讓Google來托管,可以減輕我們維護伺服器的工作量,感謝Google!

4、修改日志;

2007/11/23 v0.1b 完成,進入修訂階段;

5、參考文檔;

apache2 官方文檔 httpd.apache.org

vbb 程式參考: 《The quick 'n' dirty ultra simple vBulletin SEO hack》

drupal 參考:drupal.org

6、相關文檔;

《關于Apache (httpd)伺服器防DDOS子產品mod_evasive的使用說明》

《簡易WWW伺服器的架設》

《apache2 安裝與配置》