通過 Apache 配置 Rewrite 支持 .htaccess
【轉(zhuǎn)】 通過 Apache 配置 Rewrite 支持 .htaccess2011-03-01 14:16轉(zhuǎn)載自 分享最終編輯 ruhaole 1. 首先確定您使用的 Apache 版本,及是否加
【轉(zhuǎn)】 通過 Apache 配置 Rewrite 支持 .htaccess
2011-03-01 14:16
轉(zhuǎn)載自 分享
最終編輯 ruhaole 1. 首先確定您使用的 Apache 版本,及是否加載了 mod_rewrite 模塊。及是否加載了 mod_rewrite 模塊 . mod_rewrite 的是一個(gè) Apache 模塊,可使服務(wù)器操縱請(qǐng)求的網(wǎng)址。根據(jù)一系列規(guī)則對(duì)傳入的網(wǎng)址進(jìn)行檢查,規(guī)則中包含一個(gè)正則表達(dá)式來檢測(cè)特定的格式。 如果在地址中發(fā)現(xiàn)了一個(gè)格式,并且滿足適當(dāng)?shù)臈l件,該格式就會(huì)被一個(gè)替代的字符串或者是動(dòng)作取代。
Apache 1.x 的用戶請(qǐng)檢查 conf/httpd.conf 中是否存在如下兩段代碼:
清單 1. Apache 1.x 的加載模塊代碼
LoadModule rewrite_module libexec/mod_rewrite.so
AddModule mod_rewrite.c
Apache 2.x 的用戶請(qǐng)檢查 conf/httpd.conf 中是否存在如下一段代碼:
清單 2. Apache 2.x 的加載模塊代碼
LoadModule rewrite_module modules/mod_rewrite.so
如果存在,那么在配置文件(通常就是 conf/httpd.conf)中加入如下代碼。此時(shí)請(qǐng)務(wù)必注意,如果網(wǎng)站使用通過虛擬主機(jī)來定義,請(qǐng)務(wù)必加到虛擬主機(jī)配置,即
Apache 配置:
支持httpd.conf 配置和目錄 .htaccess配置
啟用rewrite
# LoadModule rewrite_module modules/mod_rewrite.so
去除前面的 #
LoadModule rewrite_module modules/mod_rewrite.so
啟用.htaccess
AllowOverride None 修改為: AllowOverride All
----------------------------------------------------------
,--------------------
讓apache 服務(wù)器支持.htaccess
如何讓自己的本地APACHE 服務(wù)器支持”.htaccess”呢? 其實(shí)只要簡單修改一下apache 的httpd.conf 設(shè)置就可以讓APACHE 支 持.htaccess 了。打開httpd.conf 文件用文本編輯器打開后, 查找
Options FollowSymLinks
AllowOverride None
改為
Options FollowSymLinks
AllowOverride All
就可以了。
RewriteEngine On
RewriteBase /
偽靜態(tài)
RewriteRule ^(article)-([0-9] ).htm$ $1.php?id=$2
根據(jù)IP 阻止用戶訪問
order allow,deny
deny from 127.0.0.1
allow from all
Redirects 轉(zhuǎn)向
Redirect /article.php http://www.baidu.com
防止.htaccess 文件被查看
order allow,deny
deny from all
制作錯(cuò)誤提醒頁面
ErrorDocument 404 http://www.baidu.com
添加 MIME 類型
AddType application/x-shockwave-flash swf
Tips: 設(shè)置類型為 application/octet-stream 將提示下載
改變?nèi)笔〉氖醉撐募?/p>
DirectoryIndex aa.html index.html index.pl default.htm
,防止目錄列表時(shí)顯示
IndexIgnore *
RewriteEngine On
RewriteBase /
RewriteCond {REQUEST_FILENAME} -s [OR]
RewriteCond {REQUEST_FILENAME} -l [OR]
RewriteCond {REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^([0-9a-zA-Z.] )$ demo.php?id=$1
mod_rewrite 規(guī)則修正符
1) R 強(qiáng)制外部重定向
2) F 禁用URL, 返回403HTTP 狀態(tài)碼。
3) G 強(qiáng)制URL 為GONE ,返回410HTTP 狀態(tài)碼。
4) P 強(qiáng)制使用代理轉(zhuǎn)發(fā)。
5) L 表明當(dāng)前規(guī)則是最后一條規(guī)則,停止分析以后規(guī)則的重寫。
6) N 重新從第一條規(guī)則開始運(yùn)行重寫過程。
7) C 與下一條規(guī)則關(guān)聯(lián)
如果規(guī)則匹配則正常處理,以下修正符無效
8) T=MIME-type(force MIME type) 強(qiáng)制MIME 類型
9) NS 只用于不是內(nèi)部子請(qǐng)求
10) NC 不區(qū)分大小寫
11) QSA 追加請(qǐng)求字符串
12) NE 不在輸出轉(zhuǎn)義特殊字符 =$1 等價(jià)于 =$1
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]
1、Rewrite 規(guī)則簡介:
,Rewirte 主要的功能就是實(shí)現(xiàn)URL 的跳轉(zhuǎn),它的正則表達(dá)式是基于Perl 語言??苫诜?wù)器級(jí)的(httpd.conf)和目錄級(jí)的 (.htaccess)兩種方式。如果要想用到rewrite 模塊,必須先安裝或加載rewrite 模塊。方法有兩種一種是編譯apache 的時(shí)候就直接 安裝rewrite 模塊,別一種是編譯apache 時(shí)以DSO 模式安裝apache, 然后再利用源碼和apxs 來安裝rewrite 模塊。
基于服務(wù)器級(jí)的(httpd.conf)有兩種方法,一種是在httpd.conf 的全局下直接利用RewriteEngine on來打開rewrite 功能; 另一種是在局部里利用RewriteEngine on來打開rewrite 功能, 下面將會(huì)舉例說明,需要注意的是, 必須在每個(gè)virtualhost 里用RewriteEngine on來打開rewrite 功能。否則virtualhost 里沒有RewriteEngine on它里面的規(guī)則也不會(huì)生效。
基于目錄級(jí)的(.htaccess),要注意一點(diǎn)那就是必須打開此目錄的FollowSymLinks 屬性且在.htaccess 里要聲明RewriteEngine on。
2、舉例說明:
下面是在一個(gè)虛擬主機(jī)里定義的規(guī)則。功能是把client 請(qǐng)求的主機(jī)前綴不是www.colorme.com 和203.81.23.202都跳轉(zhuǎn)到 主機(jī)前綴為http://www.colorme.com.cn,避免當(dāng)用戶在地址欄寫入http://colorme.com.cn時(shí)不能以會(huì)員方式登 錄網(wǎng)站。
NameVirtualHost 192.168.100.8:80
ServerAdmin webmaster@colorme.com.cn
DocumentRoot "/web/webapp"
ServerName www.colorme.com.cn
ServerName colorme.com.cn
RewriteEngine on #打開rewirte 功能
RewriteCond {HTTP_HOST} !^www.colorme.com.cn [NC] #聲明Client 請(qǐng)求的主機(jī)中前綴不是www.colorme.com.cn,[NC]的意思是忽略大小寫
RewriteCond {HTTP_HOST} !^203.81.23.202 [NC] #聲明Client 請(qǐng)求的主機(jī)中前綴不是203.81.23.202,[NC]的意思是忽略大小寫
RewriteCond {HTTP_HOST} !^$ #聲明Client 請(qǐng)求的主機(jī)中前綴不為空,[NC]的意思是忽略大小寫
RewriteRule ^/(.*) http://www.colorme.com.cn/ [L]
#含義是如果Client 請(qǐng)求的主機(jī)中的前綴符合上述條件,則直接進(jìn)行跳轉(zhuǎn)到http://www.colorme.com.cn/,[L]意味著立即停 止重寫操作,并不再應(yīng)用其他重寫規(guī)則。這里的.*是指匹配所有URL 中不包含換行字符,()括號(hào)的功能是把所有的字符做一個(gè)標(biāo)記,以便于后面的應(yīng)用. 就是 引用前面里的(.*)字符。
例二. 將輸入 folio.test.com 的域名時(shí)跳轉(zhuǎn)到profile.test.com
listen 8080
NameVirtualHost 10.122.89.106:8080
ServerAdmin webmaster@colorme.com.cn
DocumentRoot "/usr/local/www/apache22/data1/"
ServerName profile.test.com
RewriteEngine on
,RewriteCond {HTTP_HOST} ^folio.test.com [NC]
RewriteRule ^/(.*) http://profile.test.com/ [L]
3.Apache mod_rewrite規(guī)則重寫的標(biāo)志一覽
1) R[=code](force redirect) 強(qiáng)制外部重定向
強(qiáng)制在替代字符串加上http://thishost[:thisport]/前綴重定向到外部的URL. 如果code 不指定,將用缺省的302 HTTP 狀態(tài)碼。
2) F(force URL to be forbidden)禁用URL, 返回403HTTP 狀態(tài)碼。
3) G(force URL to be gone) 強(qiáng)制URL 為GONE ,返回410HTTP 狀態(tài)碼。
4) P(force proxy) 強(qiáng)制使用代理轉(zhuǎn)發(fā)。
5) L(last rule) 表明當(dāng)前規(guī)則是最后一條規(guī)則,停止分析以后規(guī)則的重寫。
6) N(next round) 重新從第一條規(guī)則開始運(yùn)行重寫過程。
7) C(chained with next rule) 與下一條規(guī)則關(guān)聯(lián)
如果規(guī)則匹配則正常處理,該標(biāo)志無效,如果不匹配,那么下面所有關(guān)聯(lián)的規(guī)則都跳過。
8) T=MIME-type(force MIME type) 強(qiáng)制MIME 類型
9) NS (used only if no internal sub-request) 只用于不是內(nèi)部子請(qǐng)求
10) NC(no case) 不區(qū)分大小寫
11) QSA(query string append) 追加請(qǐng)求字符串
12) NE(no URI escaping of output) 不在輸出轉(zhuǎn)義特殊字符
例如:RewriteRule /foo/(.*) /bar?arg=P1=$1 [R,NE] 將能正確的將/foo/zoo轉(zhuǎn)換成/bar?arg=P1=zed
13) PT(pass through to next handler) 傳遞給下一個(gè)處理
例如:
RewriteRule ^/abc(.*) /def$1 [PT] # 將會(huì)交給/def規(guī)則處理
Alias /def /ghi
14) S=num(skip next rule(s)) 跳過num 條規(guī)則
15) E=VAR:VAL(set environment variable) 設(shè)置環(huán)境變量
4.Apache rewrite例子集合
在 httpd 中將一個(gè)域名轉(zhuǎn)發(fā)到另一個(gè)域名虛擬主機(jī)世界近期更換了域名,新域名為 www.wbhw.com, 更加簡短好記。這時(shí)需要將原來的域名webhosting-world.com, 以及論壇所在地址 webhosting-world.com/forums/定向到新的域名,以便用戶可以找到,并且使原來的論壇 URL 繼續(xù)有效而不出現(xiàn) 404 未找到,比如原來的
在中添加下面的重定向規(guī)則:
RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond {REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com
,RewriteCond {REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]
添加了上面的規(guī)則以后, 里的全部內(nèi)容如下:
ServerAlias webhosting-world.com
ServerAdmin admin@webhosting-world.com
DocumentRoot /path/to/webhosting-world/root
ServerName www.webhosting-world.com
RewriteEngine On
# Redirect webhosting-world.com/forums to bbs.wbhw.com
RewriteCond {REQUEST_URI} ^/forums/
RewriteRule /forums/(.*) http://bbs.wbhw.com/$1 [R=permanent,L]
# Redirect webhosting-world.com to wbhw.com
RewriteCond {REQUEST_URI} !^/forums/
RewriteRule /(.*) http://www.wbhw.com/$1 [R=permanent,L]
URL 重定向
例子一:
1.http://www.zzz.com/xxx.php-> http://www.zzz.com/xxx/
2.http://yyy.zzz.com-> http://www.zzz.com/user.php?username=yyy 的功能
RewriteEngine On
RewriteCond {HTTP_HOST} ^www.zzz.com
RewriteCond {REQUEST_URI} !^user.php$
RewriteCond {REQUEST_URI} .php$
RewriteRule (.*).php$ http://www.zzz.com/$1/ [R]
RewriteCond {HTTP_HOST} !^www.zzz.com
RewriteRule ^(. ) {HTTP_HOST} [C]
RewriteRule ^([^.] ).zzz.com http://www.zzz.com/user.php?username=$1
例子二:
/type.php?typeid=* --> /type*.html
/type.php?typeid=*&page=* --> /type*page*.html
RewriteRule ^/type([0-9] ).html$ /type.php?typeid=$1 [PT]
RewriteRule ^/type([0-9] )page([0-9] ).html$ /type.php?typeid=$1&page=$2 [PT]
5. 使用Apache 的URL Rewrite配置多用戶虛擬服務(wù)器
要實(shí)現(xiàn)這個(gè)功能,首先要在DNS 服務(wù)器上打開域名的泛域名解析(自己做或者找域名服務(wù)商做)。比如,我就把 *.semcase.com和 *.semcase.cn全部解析到了我的這臺(tái)Linux Server上。
然后,看一下我的Apache 中關(guān)于*.semcase.com的虛擬主機(jī)的設(shè)定。
,#*.com,*.osall.net
ServerAdmin webmaster@semcase.com
DocumentRoot /home/www/www.semcase.com
ServerName dns.semcase.com
ServerAlias dns.semcase.com semcase.com semcase.net *.semcase.com *.semcase.net
CustomLog /var/log/httpd/osa/access_log.log" common
ErrorLog /var/log/httpd/osa/error_log.log"
AllowOverride None
Order deny,allow
#AddDefaultCharset GB2312
RewriteEngine on
RewriteCond {HTTP_HOST} ^[^.] .osall.(com|net)$
RewriteRule ^(. ) {HTTP_HOST}$1 [C]
RewriteRule ^([^.] ).osall.(com|net)(.*)$
/home/www/www.semcase.com/sylvan$3?un=$1&{QUERY_STRING} [L]
在這段設(shè)定中,我把*.semcase.net和*.semcase.com 的Document Root都設(shè)定到了 /home/www/www.semcase.com 但是,繼續(xù)看下去,看到... 配置了嗎?在這里我就配置了URL Rewrite規(guī)則。
RewriteEngine on #打開URL Rewrite功能
RewriteCond {HTTP_HOST} ^[^.] .osall.(com|net)$ #匹配條件,如果用戶輸入的URL 中主機(jī)名是類似
xxxx.semcase.com 或者 xxxx.semcase.cn 就執(zhí)行下面一句
RewriteRule ^(. ) {HTTP_HOST}$1 [C] #把用戶輸入完整的地址(GET 方式的參數(shù)除外)作為參數(shù)傳給下一個(gè)規(guī)則,
[C]是Chain 串聯(lián)下一個(gè)規(guī)則的意思
RewriteRule ^([^.] ).osall.(com|net)(.*)$ /home/www/dev.semcase.com/sylvan$3?un=$1&{QUERY_STRING} [L] # 最關(guān)鍵的是這一句,使用證則表達(dá)式解析用戶輸入的URL 地址,把主機(jī)名中的用戶名信息作為名為un 的參數(shù)傳給/home/www /dev.semcase.com目錄下的腳本,并在后面跟上用戶輸入的GET 方式的傳入?yún)?shù)。并指明這是最后一條規(guī)則([L]規(guī)則)。注意,在這一句中 指明的重寫后的地址用的是服務(wù)器上的絕對(duì)路徑,這是內(nèi)部跳轉(zhuǎn)。如果使用http://xxxx這樣的URL 格式,則被稱為外部跳轉(zhuǎn)。使用外部跳轉(zhuǎn)的話,瀏 覽著的瀏覽器中的URL 地址會(huì)改變成新的地址,而使用內(nèi)部跳轉(zhuǎn)則瀏覽器中的地址不發(fā)生改變,看上去更像實(shí)際的二級(jí)域名虛擬服務(wù)器。
Apache 下禁止特定目錄PHP ,特定虛擬主機(jī)不支持PHP
有兩種方式:方式一:.htaccess 控制,適用于沒有服務(wù)器管理權(quán)限。在可寫文件夾的目錄下,建一個(gè).htaccess 文件,內(nèi)容為:
Order allow,deny
Deny from all
方式二:修改Apache 配置文件,適用于有服務(wù)器管理權(quán)限。配置中增加如下內(nèi)容:
Order allow,deny
Deny from all
這樣只特定目錄不支持php ,其他不受影響