轉(zhuǎn)載請(qǐng)注明出處: 網(wǎng)際網(wǎng)
如何設(shè)置301重定向代碼大全
很多站長(zhǎng)都知道頂級(jí)域名要做301但是很少有人會(huì)做。一個(gè)細(xì)節(jié)問(wèn)題,今天總結(jié)了一下301重定向的方法,發(fā)出來(lái)以供參考??!今天就是教大家如何設(shè)置真正的全站301重定向,把wjdiy.com下的所有頁(yè)面301資源重定向到www. wjdiy.com 相對(duì)應(yīng)的頁(yè)面。
第一種情況,windows系統(tǒng)的服務(wù)器或者vps
IIS下301設(shè)置
Internet信息服務(wù)管理器 -> 虛擬目錄 -> 重定向到URL,輸入需要轉(zhuǎn)向的目標(biāo)URL,并選擇“資源的永久重定向”。
第二種情況,Linux的主機(jī)。
做整站301跳轉(zhuǎn),只需要修改文件httpd.conf或者.htaccess文件保存就可以了:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^wjdiy.com [NC]
RewriteRule ^(.*)$ http://news.rovide8.cn/$1 [L,R=301]
第三種情況,虛擬主機(jī)如何實(shí)現(xiàn)
虛擬主機(jī)可以利用httpd.ini文件為網(wǎng)站設(shè)置301永久重定向
1、網(wǎng)站服務(wù)器是IIS,在httpd.ini文件開始處加入以下規(guī)則:
RewriteCond Host: ^xxxx\.com$
RewriteRule (.*) http\://www\.xxxx\.com$1 [I,RP]
如果不存在httpd.ini,可以新建一個(gè),添加如下代碼:
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
RewriteCond Host: ^xxxx\.com$
RewriteRule (.*) http\://www\.xxxx\.com$1 [I,RP]
2、若網(wǎng)站服務(wù)器是Apache,新建.htaccess文件,輸入下列內(nèi)容(需要開啟mod_rewrite)
在.htaccess文件開始處加入一下規(guī)則
RewriteCond %{http_host} ^wjdiy.com [NC]
RewriteRule ^(.*)$ http://news.rovide8.cn/$1 [R=301,L]
附、httpd.ini多域名301跳轉(zhuǎn)代碼
[ISAPI_Rewrite]
# 3600 = 1 hour
CacheClockRate 3600
RepeatLimit 32
RewriteCond Host: ^xxxx2\.com$
RewriteRule (.*) http\://www\.xxxx2\.com$1 [I,RP]
RewriteCond Host: ^www\.xxxx2\.com$
RewriteRule (.*) http\://www\.xxxx1\.com$1 [I,RP]
RewriteCond Host: ^xxxx1\.com$
RewriteRule (.*) http\://www\.xxxx1\.com$1 [I,RP]
把下面一段代碼 保存為 httpd.ini 放到web里。網(wǎng)址換成自己。實(shí)現(xiàn)301跳轉(zhuǎn).
[ISAPI_Rewrite]
CacheClockRate 3600
RepeatLimit 32
RewriteCond Host: ^wjdiy.com
RewriteRule (.) http://news.rovide8.cn$1 [RP]
下面是.htaccess的代碼
RewriteEngine On
RewriteCond %{HTTP:Host} ^wjdiy.com$
RewriteRule (.) http://news.rovide8.cn/$1 [NC,R=301]
網(wǎng)站內(nèi)容地址更換,最好的辦法就是301重定向,重定向不單有利于搜索引擎優(yōu)化更能有效的提升用戶體驗(yàn)度。網(wǎng)際電腦建議只要非暫時(shí)性的地址更換都使用301進(jìn)行重定向,下面是我收集的一些301重定向代碼分享下。
1、301重定向之ASP
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://news.rovide8.cn"
%>
2、301重定向之PHP
<?
Header( "HTTP/1.1 301 Moved Permanently" ) ;
Header( "Location: http://news.rovide8.cn" );
?>
3、301重定向之NET
<script runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader ("Location","http://news.rovide8.cn");
}
</script>
4、301重定向之IIS
Internet信息服務(wù)管理器 -> 虛擬目錄 -> 重定向到URL,輸入需要轉(zhuǎn)向的目標(biāo)URL,并選擇"資源的永久重定向"。