所属分类:PHP工具与代码
301转向(或叫301重定向,301跳转)是当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种,表示本网页永久性转移到另一个地址。
其它常见的状态码还包括,200表示一切正常,404网页找不到,302暂时转向,等等。
使用 301 重定向将您原来网站上的所有网页永久重定向至新网站。 这可以告诉搜索引擎和用户您的网站已永久迁移。是符合搜索引擎友好的,目前最安全的网址域名更换。
如果您新旧网站的网页无法一一对应,可尝试确保您原来网站上的每一个网页至少会重定向至一个具有相似内容的新网页。
301的另个重要用处是网址规范化。即选择你希望的唯一的首页网址。
以下内容转载自http://www.im286.com/thread-6853244-1-1
NOTE: In the code examples below, replace "oldpagename" with the name of your old Web page from which you want to redirect traffic and replace "newpage" with the name of the new Web page to which you want to redirect traffic.
PHP语言
Save this as oldpagename.php
保存为你已失效网站的 文件名.php
[code]<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/newpage.html");
exit();
?>[/code]
ASP语言
Save this as oldpagename.asp
[code]<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.new-url.com"
%>[/code]
ASP.NET语言
Save this as oldpagename.aspx
[code]<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
</script>[/code]
ColdFusion
Save this as oldpagename.cfm
[code]<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.newdomain.com/newpage.html">[/code]
.htaccess文件
When using a Linux server with the Apache Mod-Rewrite module enabled, you can create a .htaccess file to ensure that all requests to coolexample.com will redirect to www.coolexample.com, where "coolexample.com" is your domain. Save the ..htaccess file in your old website's root directory, which is the same directory as your index file. You can create a .htaccess file with the following code:
[code]RewriteEngine on
rewritecond %{http_host} coolexample.com [nc]
rewriterule (.*)$ http://www.coolexample.com/$1 [r=301,nc][/code]
IIS with Your Dedicated or Virtual Dedicated Server
When using a Windows server, you can redirect to a 301 page using IIS.
also like this:
# ISAPI_Rewrite 3.0版本
[code]RewriteCond %{HTTP:Host} ^zpxc.com$
RewriteRule (.*) http://www.zpxc.com$1 [NC,R=301][/code]
# ISAPI_Rewrite 1.3 版本(302状态码)
[code]RewriteCond Host: ^zpxc\.com$
RewriteRule (.*) http\://www\.zpxc\.com$1 [I,R][/code]
# ISAPI_Rewrite 2.x版本
[code]RewriteCond Host: ^zpxc.com$
RewriteRule (.*) http://www.zpxc.com$1 [I,RP][/code]
In the Internet Services Manager, select the file or folder you want to redirect.
From the right-click menu, select a redirection to a URL.
Specify the file name of the page to which you want to redirect.
Select The exact URL entered above.
Select A permanent redirection for this resource.
Click Apply.
其它常见的状态码还包括,200表示一切正常,404网页找不到,302暂时转向,等等。
使用 301 重定向将您原来网站上的所有网页永久重定向至新网站。 这可以告诉搜索引擎和用户您的网站已永久迁移。是符合搜索引擎友好的,目前最安全的网址域名更换。
如果您新旧网站的网页无法一一对应,可尝试确保您原来网站上的每一个网页至少会重定向至一个具有相似内容的新网页。
301的另个重要用处是网址规范化。即选择你希望的唯一的首页网址。
以下内容转载自http://www.im286.com/thread-6853244-1-1
NOTE: In the code examples below, replace "oldpagename" with the name of your old Web page from which you want to redirect traffic and replace "newpage" with the name of the new Web page to which you want to redirect traffic.
PHP语言
Save this as oldpagename.php
保存为你已失效网站的 文件名.php
[code]<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/newpage.html");
exit();
?>[/code]
ASP语言
Save this as oldpagename.asp
[code]<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.new-url.com"
%>[/code]
ASP.NET语言
Save this as oldpagename.aspx
[code]<script language="c#" runat="server">
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.new-url.com");
}
</script>[/code]
ColdFusion
Save this as oldpagename.cfm
[code]<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.newdomain.com/newpage.html">[/code]
.htaccess文件
When using a Linux server with the Apache Mod-Rewrite module enabled, you can create a .htaccess file to ensure that all requests to coolexample.com will redirect to www.coolexample.com, where "coolexample.com" is your domain. Save the ..htaccess file in your old website's root directory, which is the same directory as your index file. You can create a .htaccess file with the following code:
[code]RewriteEngine on
rewritecond %{http_host} coolexample.com [nc]
rewriterule (.*)$ http://www.coolexample.com/$1 [r=301,nc][/code]
IIS with Your Dedicated or Virtual Dedicated Server
When using a Windows server, you can redirect to a 301 page using IIS.
also like this:
# ISAPI_Rewrite 3.0版本
[code]RewriteCond %{HTTP:Host} ^zpxc.com$
RewriteRule (.*) http://www.zpxc.com$1 [NC,R=301][/code]
# ISAPI_Rewrite 1.3 版本(302状态码)
[code]RewriteCond Host: ^zpxc\.com$
RewriteRule (.*) http\://www\.zpxc\.com$1 [I,R][/code]
# ISAPI_Rewrite 2.x版本
[code]RewriteCond Host: ^zpxc.com$
RewriteRule (.*) http://www.zpxc.com$1 [I,RP][/code]
In the Internet Services Manager, select the file or folder you want to redirect.
From the right-click menu, select a redirection to a URL.
Specify the file name of the page to which you want to redirect.
Select The exact URL entered above.
Select A permanent redirection for this resource.
Click Apply.