一 開啟 Apache 的設定檔 httpd.conf 。
二 將#LoadModule rewrite_module modules/mod_rewrite前面的#去掉
三 在 httpd.conf中添加:
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteCond %{ENV:SCRIPT_URL} (?:index|dispbbs)[-0-9]+\.html
RewriteRule ^(.*?(?:index|dispbbs))-([-0-9]+)\.html$ $1.php?__is_apache_rewrite=1&__rewrite_arg=$2
</IfModule>
四 要實現asp文章URL到php文章的映射,在 第三步的<IfModule mod_rewrite.c>和</IfModule>之間添加:
RewriteMap tolowercase int:tolower
RewriteCond %{QUERY_STRING} (?:boardid|page|id|replyid|star|skin)\=\d+ [NC]
RewriteRule ^(.*(?:index|dispbbs))\.asp$ $1.php?${tolowercase:%{QUERY_STRING}}&__is_apache_rewrite=1
五 儲存httpd.conf並重啟Apache。
六、mod_rewrite 簡介
Rewirte主要的功能就是實現URL的跳轉和隱藏真真實位址,基於Perl語言的Regex規範。平時協助我們實現偽靜態,擬目錄,網域名稱跳轉,防止盜鏈等等
七、mod_rewrite 規則的使用
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.php100.com [NC]
RewriteRule ^/(.*) http://www.php100.com/ [L]
--------
RewriteEngine on
RewriteRule ^/test([0-9]*).html$ /test.php?id=$1
RewriteRule ^/new([0-9]*)/$ /new.php?id=$1 [R]
八、mod_rewrite 規則修正符
1) R 強制外部重新導向
2) F 禁用URL,返回403HTTP狀態代碼。
3) G 強制URL為GONE,返回410HTTP狀態代碼。
4) P 強制使用代理轉寄。
5) L 表明當前規則是最後一條規則,停止分析以後規則的重寫。
6) N 重新從第一條規則開始運行重寫過程。
7) C 與下一條規則關聯8) T=MIME-type(force MIME type) 強制MIME類型
9) NS 只用於不是內部子請求
10) NC 不區分大小寫
11) QSA 追加請求字串
12) NE 不在輸出轉義特殊字元 \%3d$1 等價於 =$1
Apache虛擬機器主機下使用Rewrite實現Discuz偽靜態配置
第一步 :當然是配置Apache支援Rewrite功能,這個不多說了,就是:
LoadModule rewrite_module modules/mod_rewrite.so
原來有注釋的就去掉注釋,原來沒有的添加上。
上面的配置中:
1.Options FollowSymLinks 這一步的配置必須有。
2.AllowOverride all 原來None的要改成all。
這兩個配置缺一不可。
第三步 ,大家就比較熟悉了也不多說了,
就是在對應的目錄下面添加.htaccess檔案,下面以discuz為例說明:
RewriteEngine On
RewriteBase /
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
第四步 :配置Discuz使用偽靜態:
登入後台管理,進入“全域”=》“最佳化配置”=》“搜尋引擎最佳化”在原來URL靜態化的地方選中,然後提交即可。
配置完畢,去discuz前台訪問就可以了。原來的php都變成html了。
在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 ^/home/(space|network)-(.+)\.html$ /home/$1\.php\?rewrite=$2
RewriteRule ^/home/(space|network)\.html$ /home/$1\.php
RewriteRule ^/home/([0-9]+)$ /home/space\.php\?uid=$1
RewriteRule ^(.*)/tag-(.+)\.html$ $1/tag.php?name=$2
</IfModule>
此文適用於Apache虛擬機器主機使用者。
在開始以下設定之前,首先應該確認伺服器支援mod_rewrite以及支援對網站目錄中.htaccess的檔案解析,否則即便按照下面的方法設定好了,也無法使用。點擊查看Apache下開啟mod_rewrite的方法。
Apache下開啟mod_rewrite
伺服器環境:Windows Server 2003 SP2 + Apache 2.0.59
開啟方法:
用文字編輯器開啟Apache安裝目錄\conf\httpd.conf,找到“#LoadModule rewrite_module modules/mod_rewrite.so”這行,去掉前面的“#”;繼續尋找“AllowOverride None”,修改為“AllowOverride All”;然後,重啟Apache即可。
檢查論壇所在目錄中是否存在.htaccess 檔案,如果不存在,則手工建立此檔案。在Win32系統下,無法直接建立.htaccess 檔案,則可以從其他系統中拷貝一份,也可以點擊這裡下載我做的一個,解壓縮後即可使用。編輯並修改.htaccess檔案,添加以下內容 : 複製代碼 代碼如下:# 將 RewriteEngine 模式開啟
RewriteEngine On
# 修改以下語句中的 /discuz 為你的論壇目錄位址,如果程式放在根目錄中,請將 /discuz 修改為 /
RewriteBase /discuz
# Rewrite 系統規則請勿修改
RewriteRule ^archiver/([a-z0-9\-]+\.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 ^profile-(username|uid)-(.+)\.html$ viewpro.php?$1=$2
新增內容時,請遵照上面的提示,修改論壇所在的路徑,然後儲存。將.htaccess檔案上傳到論壇所在的目錄中。進入論壇系統設定,根據需要開啟URL靜態化功能。
-=======================================================
注意下面必須得 複製代碼 代碼如下:NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin webmaster@domain.com
DocumentRoot "E:\aaa\wwwroot"
ServerName *
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/archiver/([a-z0-9-]+.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 ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3
RewriteRule ^ss-xs/([0-9]+)/spacelist(.*)$ /index.php?$1/action_spacelist$2
RewriteRule ^ss-xs/([0-9]+)/viewspace_(.+)$ /index.php?$1/action_viewspace_itemid_$2
RewriteRule ^ss-xs/([0-9]+)/viewbbs_(.+)$ /index.php?$1/action_viewbbs_tid_$2
RewriteRule ^ss-xs/([0-9]+)/(.*)$ /index.php?$1/$2
RewriteRule ^ss-xs/([0-9]+)$ /index.php?$1
RewriteRule ^ss-xs/action_(.+)$ /index.php?action_$1
RewriteRule ^ss-xs/category_(.+)$ /index.php?action_category_catid_$1
RewriteRule ^ss-xs/itemlist_(.+)$ /index.php?action_itemlist_catid_$1
RewriteRule ^ss-xs/viewnews_(.+)$ /index.php?action_viewnews_itemid_$1
RewriteRule ^ss-xs/viewthread_(.+)$ /index.php?action_viewthread_tid_$1
RewriteRule ^ss-xs/index([.a-zA-Z0-9]*)$ /index.php
</IfModule>
</VirtualHost>
======================= 複製代碼 代碼如下:NameVirtualHost 124.129.183.146
<VirtualHost bbs.jb51.net>
ServerName bbs.jb51.net
DocumentRoot D:/WebSite/phpbbs
<Directory "D:/WebSite/phpbbs">
Options Indexes FollowSymLinks Includes
AllowOverride All
order allow,deny
Allow from all
</Directory>
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)/archiver/([a-z0-9-]+.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 ^(.*)/profile-(username|uid)-(.+).html$ $1/viewpro.php?$2=$3
RewriteRule ^ss-xs/([0-9]+)/spacelist(.*)$ /index.php?$1/action_spacelist$2
RewriteRule ^ss-xs/([0-9]+)/viewspace_(.+)$ /index.php?$1/action_viewspace_itemid_$2
RewriteRule ^ss-xs/([0-9]+)/viewbbs_(.+)$ /index.php?$1/action_viewbbs_tid_$2
RewriteRule ^ss-xs/([0-9]+)/(.*)$ /index.php?$1/$2
RewriteRule ^ss-xs/([0-9]+)$ /index.php?$1
RewriteRule ^ss-xs/action_(.+)$ /index.php?action_$1
RewriteRule ^ss-xs/category_(.+)$ /index.php?action_category_catid_$1
RewriteRule ^ss-xs/itemlist_(.+)$ /index.php?action_itemlist_catid_$1
RewriteRule ^ss-xs/viewnews_(.+)$ /index.php?action_viewnews_itemid_$1
RewriteRule ^ss-xs/viewthread_(.+)$ /index.php?action_viewthread_tid_$1
RewriteRule ^ss-xs/index([.a-zA-Z0-9]*)$ /index.php
</IfModule>
</VirtualHost>