Apache 服務器下配置(zhì)URL 靜(jìng)態化的 Rewrite 規則
如(rú)何在Apache環(huán)境下配置Rewrite規則
Apache Rewrite 規則 環境 URL 靜態化是一個利於搜索引擎的設置,通過 URL 靜態化,達到原來是動(dòng)態的(de) PHP 頁麵轉(zhuǎn)換為靜態化的 HTML 頁麵,當然,這裏的靜態化是一(yī)種假靜態,目的隻是提高搜索引擎的(de)搜索量,Comsenz 旗下的產品 Discuz!、SupeSite/X-Space、ECShop、SupeV、UCHome 等都支持此(cǐ)功能。當然這(zhè)個功能(néng)還需要服務器環境的支持,下麵介紹一下如何在(zài) Apache 服務器下配置 URL 靜態化的(de) Rewrite 規則(zé)。
當然這裏分兩種情況,一種是獨立主機用戶,這(zhè)部(bù)分用戶擁有(yǒu)對主機的管理權限,因此配置起來比(bǐ)較方便一些。(注(zhù):這裏就以(yǐ) Discuz!6.1.0 的(de) Rewrite 規則為例,稍後在後麵會列舉出其他產品的 Rewrite 規(guī)則。
首先確定您使用的 Apache 版本,及是否加載了 mod_Rewrite 模塊。
Apache 1.x 的用戶(hù)請檢查 conf/httpd.conf 中是否存在(zài)如下兩(liǎng)段代碼:
LoadModule Rewrite_module libexec/mod_Rewrite.so
AddModule mod_Rewrite.c Apache 2.x 的用戶(hù)請檢查 conf/httpd.conf 中是否存在如下一段代碼:
LoadModule Rewrite_module modules/mod_Rewrite.so
如果存在,那麽在配置文件(通常就是 conf/httpd.conf)中加入如下代碼。此時請(qǐng)務必注(zhù)意,如果網站使用(yòng)通過虛擬主機來定義,請務必加到虛擬主機配置,即(jí) <VirtualHost> 中(zhōng)去,如果(guǒ)加(jiā)在虛擬主機配置外部將可能無法使用,改(gǎi)好後將 Apache 重啟。
<IfModule mod_Rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/archiver/((fid|tid)-[\w\-]+\.html)$ $1/archiver/index.php?$2
RewriteRule ^(.*)/forum-([0-9]+)-([0-9]+)\.html$ $1/forumdisplay.php?fid=$2&page=$3
RewriteRule ^(.*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/viewthread.php?tid=$2&extra=page\%3D$4&page=$3
RewriteRule ^(.*)/space-(username|uid)-(.+)\.html$ $1/space.php?$2=$3
RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2
</IfModule>如果沒有安(ān)裝 mod_Rewrite,您可以重新編譯 Apache,並在原有 configure 的內容中加入 --enable-Rewrite=shared,然後(hòu)再在 Apache 配置文件(jiàn)中加入上(shàng)述代碼即可。
另外一種(zhǒng)用戶是 Apache 虛擬主機(jī)用戶(hù)。
在開始以下設置(zhì)之前,請首先谘詢您的空間服務商,空間是否支持 Rewrite 以及是否支持對站點目錄中 .htaccess 的文(wén)件解析,否則即便(biàn)按照下麵的方法設置好了,也無法使用。
檢查論壇所在目錄中是否存在 .htaccess 文件,如果不存在,請(qǐng)手(shǒu)工建立此文件。Win32 係統(tǒng)下,無法直接(jiē)建(jiàn)立 .htaccess 文件(jiàn),您可以從其他係統(tǒng)中拷貝(bèi)一份,或(huò)者在 Discuz.net 技術支持欄目中下載此(cǐ)文件。編輯並修改 .htaccess 文件(jiàn),添加以下(xià)內容(róng):
# 將 RewriteEngine 模式打開
RewriteEngine On
# 修改以下語句中的 /discuz 為你的論(lùn)壇目錄地址,如果程序放在根目錄中,請將 /discuz 修改為 /
RewriteBase /discuz
# Rewrite 係統規則請勿修改
RewriteRule ^archiver/((fid|tid)-[\w\-]+\.html)$ archiver/index.php?$1
RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2
RewriteRule ^thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ viewthread.php?tid=$1&extra=page\%3D$3&page=$2
RewriteRule ^space-(username|uid)-(.+)\.html$ space.php?$1=$2
RewriteRule ^tag-(.+)\.html$ tag.php?name=$1添加內(nèi)容時,請(qǐng)遵照上麵(miàn)的提示,修改論壇所在的路(lù)徑,然後保存。將 .htaccess 文件上傳到論壇所在的目(mù)錄中。然後進入論壇係統設置的搜索引擎優化,根據需要開啟 URL 靜態化功能。
下麵(miàn)簡單介紹一(yī)些其他產品的 Rewrite 規則。
SupeSite/X-Space6.0 UC規則
Apache 獨立主機用戶:
<IfModule mod_Rewrite.c>
RewriteEngine On
###Rewrite 係統(tǒng)規則請勿修改
RewriteRule ^/([0-9]+)/spacelist(.+)$ /index.php?uid/$1/action/spacelist/type$2 [L]
RewriteRule ^/([0-9]+)/viewspace(.+)$ /index.php?uid/$1/action/viewspace/itemid$2 [L]
RewriteRule ^/([0-9]+)/viewbbs(.+)$ /index.php?uid/$1/action/viewbbs/tid$2 [L]
RewriteRule ^/([0-9]+)/(.*)$ /index.php?uid/$1/$2 [L]
RewriteRule ^/([0-9]+)$ /index.php?uid/$1 [L]
RewriteRule ^/action(.+)$ /index.php?action$1 [L]
RewriteRule ^/category(.+)$ /index.php?action/category/catid$1 [L]
RewriteRule ^/viewnews(.+)$ /index.php?action/viewnews/itemid$1 [L]
RewriteRule ^/viewthread(.+)$ /index.php?action/viewthread/tid$1 [L]
RewriteRule ^/mygroup(.+)$ /index.php?action/mygroup/gid$1 [L]
</IfModule>Apache 虛擬主機用戶:
### 將 RewriteEngine 模式打開
RewriteEngine On
### 修改以下語句中的 /SupeSite 修改為你的SupeSite目錄地址,如果程序放在(zài)根目錄中,請將 /SupeSite 修改為 /
RewriteBase /
### Rewrite 係統規則請勿修改
RewriteRule ^([0-9]+)/spacelist(.+)$ index.php?uid/$1/action/spacelist/type$2 [L]
RewriteRule ^([0-9]+)/viewspace(.+)$ index.php?uid/$1/action/viewspace/itemid$2 [L]
RewriteRule ^([0-9]+)/viewbbs(.+)$ index.php?uid/$1/action/viewbbs/tid$2 [L]
RewriteRule ^([0-9]+)/(.*)$ index.php?uid/$1/$2 [L]
RewriteRule ^([0-9]+)$ index.php?uid/$1 [L]
RewriteRule ^action(.+)$ index.php?action$1 [L]
RewriteRule ^category(.+)$ index.php?action/category/catid$1 [L]
RewriteRule ^viewnews(.+)$ index.php?action/viewnews/itemid$1 [L]
RewriteRule ^viewthread(.+)$ index.php?action/viewthread/tid$1 [L]
RewriteRule ^mygroup(.+)$ index.php?action/mygroup/gid$1 [L]ECShop2.6.0 規則
Apache 獨立主機用(yòng)戶:
<IfModule mod_Rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/index.html$ $1/index.php
RewriteRule ^(.*)/category$ $1/index.php [L]
RewriteRule ^(.*)/feed-c([0-9]+).xml$ $1/feed.php?cat=$2 [L]
RewriteRule ^(.*)/feed-b([0-9]+).xml$ $1/feed.php?brand=$2 [L]
RewriteRule ^(.*)/feed.xml$ $1/feed.php
RewriteRule ^(.*)/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ $1/category.php?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6&page=$7&sort=$8&order=$9
RewriteRule ^(.*)/category-([0-9]+)-b([0-9]+)-min([0-9]+)-max([0-9]+)-attr([^-]*)\.html$ $1/category.php?id=$2&brand=$3&price_min=$4&price_max=$5&filter_attr=$6
RewriteRule ^(.*)/category-([0-9]+)-b([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ $1/category.php?id=$2&brand=$3&page=$4&sort=$5&order=$6
RewriteRule ^(.*)/category-([0-9]+)-b([0-9]+)-([0-9]+)(.*)\.html$ $1/category.php?id=$2&brand=$3&page=$4
RewriteRule ^(.*)/category-([0-9]+)-b([0-9]+)(.*)\.html$ $1/category.php?id=$2&brand=$3
RewriteRule ^(.*)/category-([0-9]+)(.*)\.html$ $1/category.php?id=$2
RewriteRule ^(.*)/goods-([0-9]+)(.*)\.html$ $1/goods.php?id=$2
RewriteRule ^(.*)/article_cat-([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)(.*)\.html$ $1/article_cat.php?id=$2&page=$3&sort=$4&order=$5
RewriteRule ^(.*)/article_cat-([0-9]+)-([0-9]+)(.*)\.html$ $1/article_cat.php?id=$2&page=$3
RewriteRule ^(.*)/article_cat-([0-9]+)(.*)\.html$ $1/article_cat.php?id=$2
RewriteRule ^(.*)/article-([0-9]+)(.*)\.html$ $1/article.php?id=$2
RewriteRule ^(.*)/brand-([0-9]+)-c([0-9]+)-([0-9]+)-(.+)-([a-zA-Z]+)\.html $1/brand.php?id=$2&cat=$3&page=$4&sort=$5&order=$6
RewriteRule ^(.*)/brand-([0-9]+)-c([0-9]+)-([0-9]+)(.*)\.html $1/brand.php?id=$2&cat=$3&page=$4
RewriteRule ^(.*)/brand-([0-9]+)-c([0-9]+)(.*)\.html $1/brand.php?id=$2&cat=$3
RewriteRule ^(.*)/brand-([0-9]+)(.*)\.html $1/brand.php?id=$2
RewriteRule ^(.*)/tag-(.*)\.html $1/search.php?keywords=$2
RewriteRule ^(.*)/snatch-([0-9])\.html$ $1/snatch.php?id=$2
RewriteRule ^(.*)/group_buy-([0-9])\.html$ $1/group_buy.php?act=view&id=$2
</IfModule>
Apache 虛擬主機用戶(hù):
RewriteEngine On
#RewriteBase /
# direct one-word access
RewriteRule ^index\.html$ index\.php [L]
RewriteRule ^category$ index\.php [L]
# access any object by its numeric identifier
RewriteRule ^feed-c([0-9]+)\.xml$ feed\.php\?cat=$1 [L]
RewriteRule ^feed-b([0-9]+)\.xml$ fe
關鍵詞:Apache,服務器,Rewrite
閱讀本文後您有什麽感想? 已有 人給出評價!
- 1
- 1
- 1
- 1
- 1
- 1