URL 靜態化是一個有利於搜尋引擎的設定,透過 URL 靜態化,達到原來是動態的 PHP 頁面轉換為靜態化的 HTML 頁面,可以提高搜尋引擎抓取,當然,這裡的靜態化是一種假靜態,目的只是提高搜尋引擎的搜尋量,下面主要介紹配置方法。
分兩種情況,一種是獨立主機使用者,這部分使用者擁有對主機的管理許可權,因此配置起來比較方便一些。
首先確定您使用的 Apache 版本,及是否載入了 mod_Rewrite 模組。
Apache 1.x 的使用者請檢查 conf/httpd.conf 中是否存在如下兩段程式碼:
LoadModule Rewrite_module libexec/mod_Rewrite.so
AddModule mod_Rewrite.c Apache 2.x 的使用者請檢查 conf/httpd.conf 中是否存在如下一段程式碼:
LoadModule Rewrite_module modules/mod_Rewrite.so
如果存在,那麼在配置檔案 (通常就是 conf/httpd.conf) 中加入如下程式碼。此時請務必注意,如果網站使用透過虛擬主機來定義,請務必加到虛擬主機配置,即 <VirtualHost> 中去,如果加在虛擬主機配置外部將可能無法使用,改好後將 Apache 重啟。
一、 Apache Web Server(獨立主機使用者)
- <IfModule mod_rewrite.c>
- RewriteEngine On
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^(.*)/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^(.*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%
- 3D$4&page=$3&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^(.*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^(.*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^(.*)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3&%1
- </IfModule>
如果沒有安裝 mod_Rewrite,您可以重新編譯 Apache,並在原有 configure 的內容中加入 --enable-Rewrite=shared,然後再在 Apache 配置檔案中加入上述程式碼即可。
二、 Apache Web Server(虛擬主機使用者)
在開始以下設定之前,請首先諮詢您的空間服務商,空間是否支援 Rewrite 以及是否支援對站點目錄中 .htaccess 的檔案解析,否則即便按照下面的方法設定好了,也無法使用。
檢查論壇所在目錄中是否存在 .htaccess 檔案,如果不存在,請手工建立此檔案。 Win32 系統下,無法直接建立 .htaccess 檔案,您可以從其他系統中複製一份,編輯並修改 .htaccess 檔案,新增以下內容:
- # 將 RewriteEngine 模式開啟
- RewriteEngine On
- # 修改以下語句中的 /discuz 為你的論壇目錄地址,如果程式放在根目錄中,請將 /discuz 修改為 /
- RewriteBase /discuz
- # Rewrite 系統規則請勿修改
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^topic-(.+).html$ portal.php?mod=topic&topic=$1&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^forum-(w+)-([0-9]+).html$ forum.php?mod=forumdisplay&fid=$1&page=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+).html$
forum.php?mod=viewthread&tid=$1&extra=page%3D$3&page=$2&%1 - RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^group-([0-9]+)-([0-9]+).html$ forum.php?mod=group&fid=$1&page=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^space-(username|uid)-(.+).html$ home.php?mod=space&$1=$2&%1
- RewriteCond %{QUERY_STRING} ^(.*)$
- RewriteRule ^archiver/(fid|tid)-([0-9]+).html$ archiver/index.php?action=$1&value=$2&%1
三、 IIS Web Server(獨立主機使用者)
說明:IIS 下的 Rewrite 需要新增元件,透過篩選器的方式實現,下面是元件的下載與配置方法:
IIS Rewrite 下載
Rewrite.zip
IIS Rewrite 配置
1 、將下載的 IIS Rewrite 元件解壓,放到任意盤上的任意目錄 (如 C:Rewrite 下)

2 、在 IIS 管理器裡選擇網站,右鍵選擇 「屬性」,如下圖所示:

在彈出的視窗裡選擇 「ISAPI 篩選器」

上圖中點選 「新增」,在彈出的視窗裡 「篩選器名稱」 填寫 「rewrite」

上圖介面中點選 「瀏覽」,選擇下載解壓後的 IIS Rewrite 元件目錄下的 Rewrite.dll 檔案

瀏覽完畢點選 「確定」

新增完畢點選 「確定

重新啟動 IIS


重新選擇該站點 => 右鍵 「屬性」=> 「ISAPI 篩選器」,如果看到狀態為向上的綠色箭頭,就說明 Rewrite 模組安裝成功了。

這個時候返回到論壇首頁隨便開啟一個版塊或者一個帖子即可看到 Discuz! 的 Rewrite 偽靜態配置成功!


四、 IIS7 Web Server(獨立主機使用者)
可以將如下的配置規則加入到 web.config 中
- <rewrite>
- <rules>
- <rule name="portal_topic">
- <match url="http://(.*/)*topic-(.+).html?*(.*)$" />
- <action type="Rewrite" url="" />
- </rule>
- <rule name="forum_forumdisplay">
- <match url="http://(.*/)*forum-(w+)-(%5B0-9%5D+).html?*(.*)$" />
- <action type="Rewrite" url="" />
- </rule>
- <rule name="forum_viewthread">
- <match url="http://(.*/)*thread-(%5B0-9%5D+)-(%5B0-9%5D+)-(%5B0-9%5D+).html?*(.*)$" />
- <action type="Rewrite"
url="{R:1}/forum.php?mod=viewthread&tid={R:2}&extra=page%3D{R:4}&page={R:3}&{R:5}"
/> - </rule>
- <rule name="group_group">
- <match url="http://(.*/)*group-(%5B0-9%5D+)-(%5B0-9%5D+).html?*(.*)$" />
- <action type="Rewrite" url="" />
- </rule>
- <rule name="home_space">
- <match url="http://(.*/)*space-(username|uid)-(.+).html?*(.*)$" />
- <action type="Rewrite" url="" />
- </rule>
- <rule name="forum_archiver">
- <match url="http://(.*/)*(fid|tid)-(%5B0-9%5D+).html?*(.*)$" />
- <action type="Rewrite" url="" />
- </rule>
- </rules>
- </rewrite>
五、 Zeus Web Server
配置規則如下:
- match URL into $ with ^(.*)/topic-(.+).html?*(.*)$
- if matched then
- set URL = $1/portal.php?mod=topic&topic=$2&$3
- endif
- match URL into $ with ^(.*)/forum-(w+)-([0-9]+).html?*(.*)$
- if matched then
- set URL = $1/forum.php?mod=forumdisplay&fid=$2&page=$3&$4
- endif
- match URL into $ with ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html?*(.*)$
- if matched then
- set URL = $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3&$5
- endif
- match URL into $ with ^(.*)/group-([0-9]+)-([0-9]+).html?*(.*)$
- if matched then
- set URL = $1/forum.php?mod=group&fid=$2&page=$3&$4
- endif
- match URL into $ with ^(.*)/space-(username|uid)-(.+).html?*(.*)$
- if matched then
- set URL = $1/home.php?mod=space&$2=$3&$4
- endif
- match URL into $ with ^(.*)/(fid|tid)-([0-9]+).html?*(.*)$
- if matched then
- set URL = $1/index.php?action=$2&value=$3&$4
- endif
六、 Nginx Web Server
配置規則如下:
- rewrite ^([^.]*)/topic-(.+).html$ $1/portal.php?mod=topic&topic=$2 last;
- rewrite ^([^.]*)/forum-(w+)-([0-9]+).html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
- rewrite ^([^.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%
- 3D$4&page=$3 last;
- rewrite ^([^.]*)/group-([0-9]+)-([0-9]+).html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
- rewrite ^([^.]*)/space-(username|uid)-(.+).html$ $1/home.php?mod=space&$2=$3 last;
- rewrite ^([^.]*)/(fid|tid)-([0-9]+).html$ $1/index.php?action=$2&value=$3 last;
- if (!-e $request_filename) {
- return 404;
- }