溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

使用ISAPI_Rewrite做實(shí)用的重定向

發(fā)布時(shí)間:2020-06-13 14:20:34 來源:網(wǎng)絡(luò) 閱讀:510 作者:gutaotao1989 欄目:開發(fā)技術(shù)

.htaccess文件的內(nèi)容如下:

RewriteCond %{HTTP_HOST} ask.xxoo.com

RewriteRule ^(index.html)?$ /ask/

RewriteRule ^(.*)list-([0-9]+)-([a-z]+)\.html$ $1/ask/list.php?catid=$2&action=$3

RewriteRule ^(.*)show-([0-9]+)\.html$ $1/ask/show.php?id=$2


分析代碼:

RewriteCond 后面的 %{HTTP_HOST} 是域名 

RewriteCond %{HTTP_HOST} space.xxoo.com 表示地址欄中的域名是否等于space.xxoo.com這里也可以使用正則 如:RewriteCond %{HTTP_HOST} space([1-9]).xxoo.com 這匹配的url包含了space1.xxoo.com  space2.xxoo.com space2.xxoo.com  .......space9.xxoo.com 這9個(gè)域名


RewriteRule ^(index.html)?$ /ask/   

匹配的是網(wǎng)站但域名ask.xxoo.com或ask.xxoo.com/index.html這兩個(gè)地址: 重定向的是ask.xxoo.com/ask


RewriteRule ^(.*)list-([0-9]+)-([a-z]+)\.html$ $1/ask/list.php?catid=$2&action=$3

匹配地址包含

ask.xxoo.com/list-3-all.html 重定向的是ask.xxoo.com/ask/list.php?catid=3&action=all

ask.xxoo.com/list-45-high.html 重定向的是ask.xxoo.com/ask/list.php?catid=45&action=hight


RewriteRule ^(.*)show-([0-9]+)\.html$ $1/ask/show.php?id=$2

匹配地址包含

ask.xxoo.com/show-3.html 重定向的是ask.xxoo.com/ask/show.php?id=3

ask.xxoo.com/show-21.html 重定向的是ask.xxoo.com/ask/show.php?id=21

。。。。。


自己現(xiàn)在服務(wù)器在用的 ISAPI_Rewrite 3.x.x

<IfModule mod_rewrite.c>

RewriteEngine On

RewriteRule ^index.html$ / [NC,L,R=301,O] 

</IfModule>

匹配的意義:把根目錄下面的index.html 默認(rèn)跳轉(zhuǎn)到不帶index.html 防止index.html分散權(quán)重


說明:O表示對(duì)URL進(jìn)行標(biāo)準(zhǔn)化,L表示Last Rule,最后一條規(guī)則,也就是后面的重寫規(guī)則對(duì)他不起作用,防止被其他匹配的規(guī)則再次重寫。這里的路徑可以是相對(duì)路徑也可以是絕對(duì)路徑。

說明:[I,RP]:I表示忽略大小寫,RP表示使用301轉(zhuǎn)向,以上都是整個(gè)域名重定向。



.htaccess的301重定向代碼


把不帶www的域名301到帶www的域名


RewriteEngine On

RewriteCond %{http_host} ^example.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]


“RewriteEngine On”,開啟 Rewrite 規(guī)則開關(guān);

“RewriteCond”指重寫的條件。后面的字符串通過正則表達(dá)式進(jìn)行匹配,匹配字符串以 ^ 開頭并以 $ 結(jié)尾。此處 %{http_host} 獲取當(dāng)前主機(jī)名稱。條件為“當(dāng)主機(jī)名稱為example.com”時(shí)執(zhí)行下列的重寫規(guī)則。其中“[NC]”指不區(qū)分大小寫;

“RewriteRule”,定義一條重寫規(guī)則。此處含義:跳轉(zhuǎn)到“http://www.example.com/”接上訪問請(qǐng)求的網(wǎng)址中 example.com 后面的部分。[R=301] 指重寫為 301 重定向/跳轉(zhuǎn)([R] 單指跳轉(zhuǎn),意義等同 [R=302]),[L] 指最后一條匹配規(guī)則。


把老域名301到新域名


更換域名時(shí),老域名的權(quán)重不能浪費(fèi)了,把老域名的頂級(jí)域名和帶www的域名都要301到新域名,代碼如下


RewriteEngine On

RewriteCond %{http_host} ^(www.)?old.com$ [NC,OR]

RewriteCond %{http_host} ^new.com$ [NC]

RewriteRule ^(.*)$ http://www.new.com/$1 [R=301,L]


現(xiàn)在無論你訪問old.com,www.old.com,new.com都會(huì)301到www.new.com 夠完美了吧!而且所有的內(nèi)頁也會(huì)跟著301,接下來至少等待2個(gè)月,期間不要?jiǎng)h除原域名,靜等權(quán)重完全轉(zhuǎn)移!

需要注意的是,wordpress默認(rèn)情況下不支持該條命令,因?yàn)閣ordpress的網(wǎng)址本身就是偽靜態(tài)的,要想實(shí)現(xiàn)這個(gè)功能,必須先把wordpress的內(nèi)頁生成html文件。


==重要提醒==:IIS 6中的.htaccess 301定向


以下規(guī)則適用于 IIS 6中的 ISAPI_Rewrite 2.x 3.x 中的httpd.ini或.htaccess

注意這兒的.htaccess不是apache中的.htaccess 規(guī)則略有不同。


RewriteCond %{HTTP:Host} ^sjyhome.com$

RewriteRule (.*) http://www.sjyhome.com/$1 [NC,R=301]


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI