APACHE通過URL重寫偽靜態
APACHE通過URL重寫偽靜態
一.Apache設定獨立主機使用者
Apache 基本配置:
首先確定您使用的 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
如果沒有安裝 mod_Rewrite,您可以重新編譯 Apache,並在原有 configure 的內容中加入 --enable-Rewrite=shared
注:如果前面有#,將其去掉。
方法一:通過配置Apache配置檔案httpd.conf實現URL重寫
在配置檔案(通常就是 conf/httpd.conf)中加入如下程式碼。
<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>
注:此時請務必注意,如果網站使用通過虛擬主機來定義,請務必加到虛擬主機配置,即 <VirtualHost> 中去,如果加在虛擬主機配置外部將可能無法使用,改好後將 Apache 重啟。
方法二:通過在根目錄中的跨越配置檔案.htaccess實現URL重寫
1.
配置apache支援對 .htaccess 檔案的解析
查詢:
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
修改為:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
man對AllowOverride 的解釋:
AllowOverride controls what directives may be placed in .htaccess files.
It can be "All", "None", or any combination of the keywords:
Options FileInfo AuthConfig Limit
就是說,將None改為All,.htaccess檔案才能被支援!
2. 建立.htaccess檔案Win32 系統下,無法直接建立 .htaccess 檔案,您可以從其他系統中拷貝一份,或者在 Discuz.net 技術支援欄目中下載此檔案。(附件中可下載)
3. 編輯.htaccess檔案
# 將 RewriteEngine 模式開啟
RewriteEngine On
# .htaccess檔案路徑,如果在系統跟目錄則為RewriteBase /,如果在根目錄下的其他資料夾,如在根目錄下的test資料夾,則為RewriteBase /test,此處將.htaccess放在根目錄下。
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
租用空間使用者(多是虛擬主機使用者)
1. 首先諮詢您的空間服務商,空間是否支援 Rewrite 以及是否支援對站點目錄中 .htaccess 的檔案解析,否則即便按照下面的方法設定好了,也無法使用。
2. 建立.htaccess檔案Win32 系統下,無法直接建立 .htaccess 檔案,您可以從其他系統中拷貝一份,或者在 Discuz.net 技術支援欄目中下載此檔案。
3. 編輯.htaccess檔案
# 將 RewriteEngine 模式開啟
RewriteEngine On
# .htaccess檔案路徑,如果在系統跟目錄則為RewriteBase /,如果在根目錄下的其他資料夾,如在根目錄下的test資料夾,則為RewriteBase /test,此處將.htaccess放在根目錄下。
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
二.Rewrite 規則
上面無論是在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>
還在是檔案.htaccess中新增的:
# 將 RewriteEngine 模式開啟
RewriteEngine On
# .htaccess檔案路徑,如果在系統跟目錄則為RewriteBase /,如果在根目錄下的其他資料夾,如在根目錄下的test資料夾,則為RewriteBase /test,此處將.htaccess放在根目錄下。
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
其中都指明瞭URL重寫規則。
請看:RewriteRule ^forum-([0-9]+)-([0-9]+)\.html$ forumdisplay.php?fid=$1&page=$2
這句說明了URL為forumdisplay.php?fid=$1&page=$2可以寫成forum-([0-9]+)-([0-9]+)\.html這種模式。
如:訪問http://localhost/ forumdisplay.php?fid=1&page=2與訪問http://localhost/ forum-1-2.html的效果是一樣的!
注:這些規則是可以自己寫正則表示式隨意更改的。根據自己需要的格式。來定製URL重寫規則。
參考資料:
http://faq.comsenz.com/viewnews-12
http://www.eygle.com/digest/2005/09/apache_oeoeooaeuooa_rewrite.html