但是對(duì)于虛擬主機(jī)用戶來(lái)說(shuō),301重定向很難,因?yàn)樘摂M主機(jī)提供商一般不會(huì)提供這個(gè)設(shè)置。
但是對(duì)于安裝了ISAPI Rewrite的空間,那么301重定向就相對(duì)來(lái)說(shuō)變得簡(jiǎn)單了。
下面我們還是以實(shí)例來(lái)說(shuō)明正確使用ISAPI Rewrite做301重定向的方法吧。
1.3版的域名重定向:
代碼如下:
# For ISAPI_Rewrite 1.3 重定向域名
#重定向jb51.cn
RewriteCond Host: ^Vevb.com$
RewriteRule (.*) http://jb51.cn$1 [I,R]
#重定向www.uuwar.org
RewriteCond Host: ^www.companysz.com$
RewriteRule (.*) http://www.jb51.cn$1 [I,R]
這里我們要將Vevb.com重定向到j(luò)b51.cn
由于ISAPI Rewrite現(xiàn)在使用較多的有兩個(gè)版本2.x版跟3.x版
那么就把兩個(gè)版本的規(guī)則都寫(xiě)出來(lái),3.0版本是不兼容2.0的規(guī)則的,只是有提供規(guī)則轉(zhuǎn)換器,導(dǎo)入就可以轉(zhuǎn)換了。
代碼如下:
# For ISAPI_Rewrite 2.x
RewriteCond Host: ^maphack.org$
RewriteRule (.*) http://jb51.cn$1 [I,RP]
RewriteCond Host: ^www.maphack.org$
RewriteRule (.*) http://www.jb51.cn$1 [I,RP]
# For ISAPI_Rewrite 3.x
RewriteCond %{HTTP:Host} ^maphack.org$
RewriteRule (.*) http://jb51.cn$1 [NC,R=301] RewriteCond %{HTTP:Host} ^www.maphack.org$
RewriteRule (.*) http://www.jb51.cn$1 [NC,R=301]
說(shuō)明:[I,RP]:I表示忽略大小寫(xiě),RP表示使用301轉(zhuǎn)向,以上都是整個(gè)域名重定向。
單一頁(yè)面重定向的寫(xiě)法,將根目錄下的1.html重定向到http://www.jb51.cn/index.html:
代碼如下:
# For ISAPI_Rewrite 2.x
RewriteRule ^/1.html$ http://www.jb51.cn/index.html [I,O,RP,L]
# For ISAPI_Rewrite 3.x
RewriteRule ^/1.html$ http://www.jb51.cn/index.html [NC,L,R=301,O]
說(shuō)明:O表示對(duì)URL進(jìn)行標(biāo)準(zhǔn)化,L表示Last Rule,最后一條規(guī)則,也就是后面的重寫(xiě)規(guī)則對(duì)他不起作用,防止被其他匹配的規(guī)則再次重寫(xiě)。這里的路徑可以是相對(duì)路徑也可以是絕對(duì)路徑。