301跳轉(zhuǎn)通常用在網(wǎng)站換域名和為了保持鏈接統(tǒng)一性所用的。比如你原來的域名www.a.com現(xiàn)在換成www.b.com,用了301跳轉(zhuǎn)后,訪問www.a.com/about.html就會(huì)自動(dòng)變成www.b.com/about.html。有時(shí)我們做網(wǎng)站改版后,需要使用301重定向。下面發(fā)菜工作室就設(shè)置301的代碼吧,方便一下各位站長們:
301跳轉(zhuǎn)代碼全集(ASP|PHP|JSP|.NET)
1、IIS下301設(shè)置
Internet信息服務(wù)管理器 -> 虛擬目錄 -> 重定向到URL,輸入需要轉(zhuǎn)向的目標(biāo)URL,并選擇“資源的永久重定向”。
2、ASP下的301轉(zhuǎn)向代碼
<%@ Language=VBScript %>
<%
Response.Status=”301 Moved Permanently”
Response.AddHeader “Location”, “http://www.xxx.com/”
%>
3、ASP.Net下的301轉(zhuǎn)向代碼
<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”http://www.xxx.com/”);
}
</script>
4、PHP下的301轉(zhuǎn)向代碼
header(”HTTP/1.1 301 Moved Permanently”);
header(”Location: http://www.xxx.com/”);
exit();
5、CGI Perl下的301轉(zhuǎn)向代碼
$q = new CGI;
print $q->redirect(”http://www.xxx.com/”);
6、JSP下的301轉(zhuǎn)向代碼
<%
response.setStatus(301);
response.setHeader( “Location”,“http://www.xxx.com/” );
response.setHeader( “Connection”,“close” );
%>
7、Apache下vhosts.conf中配置301轉(zhuǎn)向
為實(shí)現(xiàn)URL規(guī)范化,SEO通常將不帶WWW的域名轉(zhuǎn)向到帶WWW域名,vhosts.conf中配置為:
<VirtualHost *:80>
ServerName www.xxx.com
DocumentRoot
</VirtualHost>
<VirtualHost *:80>
ServerName xxx.com
RedirectMatch permanent ^/(.*) http://www.xxx.com/$1
</VirtualHost>
8、Apache下301轉(zhuǎn)向代碼
新建.htaccess文件,輸入下列內(nèi)容(需要開啟mod_rewrite):
1)將不帶WWW的域名轉(zhuǎn)向到帶WWW的域名下
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.com [NC]
RewriteRule ^(.*)$ http://www.xxx.com/$1 [L,R=301]
2)重定向到新域名
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.xxx.com/$1 [L,R=301]
3)使用正則進(jìn)行301轉(zhuǎn)向,實(shí)現(xiàn)偽靜態(tài)
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^news-(.+).html$ news.php?id=$1
將news.php?id=123這樣的地址轉(zhuǎn)向到news-123.html
最后:在使用301永久性重定向命令讓多個(gè)域名指向網(wǎng)站主域名時(shí),也不會(huì)對(duì)網(wǎng)站的排名產(chǎn)生任何負(fù)面影響。希望對(duì)你有幫助。