虛擬主機(jī)怎么做301重定向?設(shè)置301重定向規(guī)則,生成文件放入到根目錄,即可實(shí)現(xiàn)虛擬主機(jī)301重定向。
什么時候需要做301重定向?一是為了規(guī)范url,集中權(quán)重;二是,網(wǎng)站改版,將舊版頁面轉(zhuǎn)移到新版頁面上。
比如,網(wǎng)站首頁的url有以下幾種:
xxx.com
www.xxx.com
xxx.com/index.html
www.xxx.com/index.html
以及http頁面和https頁面等多種。一個首頁出現(xiàn)如此多url,一是不規(guī)范,二是權(quán)重分散,這時我們就需要將所有指向首頁的url統(tǒng)一規(guī)范到某一個,比如:www.xxx.com或https://www.xxx.com/。
還有頁面的url改變了,那么我們也需要做一個301重定向,避免老的url產(chǎn)生404。
那么我們怎么做301重定向呢?要區(qū)分linux虛擬主機(jī)還是windows虛擬主機(jī),兩者301重定向規(guī)則不同。
linux虛擬主機(jī),一般偽靜態(tài)規(guī)則放入.htaccess文件。
<ifmodule mod_rewrite.c>
rewriteengine on
rewritecond %{http:from-https} !^on$ [nc]
rewritecond %{http_host} ^(www.)?xxx.com$ [nc]
rewriterule ^(.*)$ https://www.xxx.com/$1 [r=301,l]
</ifmodule># 將xxx.com和www.xxx.com跳轉(zhuǎn)到https://www.xxx.com,防止apache子站繼承上級目錄.htaccess受影響,注釋請自行刪除,不需要放入htaccess文件
如果有多個需求,如:a.com跳轉(zhuǎn)到到www.a.com;b.com跳轉(zhuǎn)到www.b.com,規(guī)則如下:
rewriteengine on
rewritecond %{http_host} ^a.com$ [nc]
rewriterule ^(.*)$ http://www.a.com/$1 [r=301,l]
rewritecond %{http_host} ^b.com$ [nc]
rewriterule ^(.*)$ http://www.b.com/$1 [r=301,l]根據(jù)自身網(wǎng)站需要添加修改,然后上傳規(guī)則到服務(wù)器wwwroot根目錄的.htaccess文件里。如沒有.htaccess文件,新建一個文本文檔,并重命名為.htaccess。
windows虛擬主機(jī),一般偽靜態(tài)規(guī)則放入到web.config文件。
<?xml version=1.0 encoding=utf-8?>
<configuration>
<system.webserver>
<rewrite>
<rules>
<rule name=301 stopprocessing=true>
<match url=^(.*)$ ignorecase=false />
<conditions logicalgrouping=matchall>
<add input={http_from_https} pattern=^on$ negate=true />
</conditions>
<action type=redirect url=https://www.xxx.com/{r:1} redirecttype=permanent />
</rule>
</rules>
</rewrite>
</system.webserver>
</configuration># www.xxx.com對應(yīng)修改為您自已的域名,注釋請自行刪除,不需要放入web.config文件內(nèi)
以上是關(guān)于“虛擬主機(jī)怎么做301重定向”的介紹,如需購買虛擬主機(jī),首推西部數(shù)碼:https://www.west.cn/services/webhosting/