URL http 重定向是一种从一个 URL 到另一个 URL 的自动 URL 更改操作。
URL 页面重定向是一种从一个 URL 到另一个 URL 的自动 URL 更改操作。
出于以下原因进行此重定向:
用户可能会从旧的外部链接或书签到达旧的 URL。
由网站管理员添加脚本。
服务器端重定向是在服务器中完成的,通过配置 Apache / IIS 服务器软件或使用 PHP / ASP / ASP.NET 脚本。
这是重定向 URL 的首选方式,因为您可以返回 HTTP 301 Moved Permanently 状态代码。
搜索引擎使用 301 状态将页面排名从旧 URL 转移到新 URL。
客户端重定向是在用户的 Web 浏览器中通过使用 HTML 元刷新标记或通过 Javascript 代码完成的。
客户端重定向不太受欢迎,因为它不返回 HTTP 301 状态代码。
域名 _ |
托管 服务器 |
重定向代码 放置 |
---|---|---|
没有改变 | 没有改变 | 同一服务器上的旧页面 |
没有改变 | 变了 | 新服务器上的旧页面 |
变了 | 没有改变 | 同一服务器上的旧页面 |
变了 | 变了 | 旧服务器上的旧页面 |
* 仅限.htaccess重定向:将重定向代码添加到 httpd.conf 文件或.htaccess文件。
状态码 | 状态码名称 | 描述 |
---|---|---|
200 | 好的 | 成功的 HTTP 请求 |
300 | 多项选择 | |
301 | 永久移动 | 永久 URL 重定向 |
302 | 成立 | 临时 URL 重定向 |
303 | 查看其他 | |
304 | 未修改 | |
305 | 使用代理服务器 | |
307 | 临时重定向 | |
404 | 未找到 | 找不到网址 |
HTTP 301 Moved Permanently状态代码表示永久 URL 重定向。
301 重定向是重定向 URL 的首选方式,因为它通知搜索引擎该 URL 已经永久移动,搜索引擎应该将新 URL 页面而不是旧 URL 页面放在搜索结果中,并转移新 URL 页面,旧 URL 页面的页面排名。
301 重定向可以跨域或在同一域上完成。
Google建议使用 301 重定向。
重定向脚本 | 重定向端 | 旧页面文件类型 | 重定向 URL 或域 | 旧 URL 服务器类型 | 301重定向支持 |
---|---|---|---|---|---|
PHP | 服务器端 | .php | 网址 | 阿帕奇 / Linux | 是的 |
ASP | 服务器端 | .asp | 网址 | IIS / Windows | 是的 |
网络 | 服务器端 | .aspx | 网址 | IIS / Windows | 是的 |
.htaccess | 服务器端 | 全部 | 网址/域名 | 阿帕奇 / Linux | 是的 |
信息系统 | 服务器端 | 全部 | 网址/域名 | IIS / Windows | 是的 |
HTML 规范链接标记 | 客户端 | .html | 网址 | 全部 | 不 |
HTML 元刷新 | 客户端 | .html | 网址 | 全部 | 不 |
HTML框架 | 客户端 | .html | 网址 | 全部 | 不 |
Javascript | 客户端 | .html | 网址 | 全部 | 不 |
查询 | 客户端 | .html | 网址 | 全部 | 不 |
redirect script - 用于重定向的脚本语言。
redirect side - 重定向发生的地方 -服务器端或客户端。
旧页面文件类型- 可以包含重定向代码脚本语言的旧 URL 页面的类型。
重定向 URL 或域- 支持单个网页的URL重定向或整个网站的域重定向。
典型的旧 URL 服务器类型- 服务器的典型软件和操作系统。
301 redirect support - 表示是否可以返回永久的 301 重定向状态响应。
将 old-page.php 代码替换为指向 new-page.php 的重定向代码。
old_page.php:
<?php
// PHP permanent URL redirection
header("Location: http://www.mydomain.com/new-page.php", true,
301);
exit();
?>
旧页面必须有 .php 文件扩展名。
新页面可以使用任何扩展名。
请参阅:PHP 重定向
.htaccess文件是 Apache 服务器的本地配置文件。
如果您有权更改 httpd.conf 文件,最好在 httpd.conf 而不是 .htaccess 文件中添加重定向指令。
从 old-page.html 永久重定向到 new-page.html。
.htaccess:
Redirect 301 /old-page.html http://www.mydomain.com/new-page.html
从所有域页面永久重定向到 newdomain.com。
.htaccess文件应该在旧网站的根目录中。
.htaccess:
Redirect 301 / http://www.newdomain.com/
请参阅:.htaccess 重定向
旧页.asp:
<%@ Language="VBScript" %>
<%
' ASP permanent URL redirection
Response.Status="301 Moved Permanently"
Response.AddHeader "Location", "http://www.mydomain.com/new-page.html"
Response.End
%>
旧页面.aspx:
<script language="C#" runat="server">
// ASP.net permanent URL redirection
private void Page_Load(object sender, EventArgs e)
{
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","http://www.mydomain.com/new-page.html");
Response.End();
}
</script>
HTML 元刷新标签重定向不返回 301 永久重定向状态代码,但被 Google 视为 301 重定向。
Replace old page with redirection code 为您要重定向到的页面的 URL。
旧页面.html:
<!-- HTML meta refresh URL redirection -->
<html>
<head>
<meta http-equiv="refresh"
content="0; url=http://www.mydomain.com/new-page.html">
</head>
<body>
<p>The page has moved to:
<a href="http://www.mydomain.com/new-page.html">this
page</a></p>
</body>
</html>
请参阅:HTML 重定向
Javascript 重定向不返回 301 永久重定向状态代码。
Replace old page with redirection code 为您要重定向到的页面的 URL。
旧页面.html:
<html>
<body>
<script type="text/javascript">
// Javascript URL redirection
window.location.replace("http://www.mydomain.com/new-page.html");
</script>
</body>
</html>
请参阅:Javascript 重定向
jQuery 重定向实际上是另一种 Javascript 重定向。
jQuery 重定向不返回 301 永久重定向状态代码。
Replace old page with redirection code 为您要重定向到的页面的 URL。
旧页面.html:
<!DOCTYPE html>
<html>
<body>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<脚本类型="text/javascript">
// jQuery URL 重定向
$(document).ready( function() {
url = "http://www.mydomain.com/new-page.html";
$( location ).attr( "href", 网址);
});
</脚本>
</正文>
</html>
请参阅:jQuery 重定向
规范链接不会重定向到首选 URL,但它可以作为大多数流量来自搜索引擎的网站的 URL 重定向的替代方法。
当有多个页面内容相似并且您想告诉搜索引擎您希望在搜索结果中使用哪个页面时,可以使用 HTML 规范链接标记。
规范链接标签可以链接到同域,也可以跨域。
将规范链接标记添加到旧页面以链接到新页面。
将规范链接标记添加到您不想让搜索引擎流量链接到首选页面的页面。
规范链接标签应添加到 <head> 部分。
旧页面.html:
<link rel="canonical" href="http://www.mydomain.com/new-page.html">
请参阅:规范 URL 链接
在框架重定向中,new-page.html 文件由 html 框架查看。
这不是真正的 URL 重定向。
帧重定向对搜索引擎不友好,因此不推荐使用。
旧页面.html:
<!-- HTML frame redirection -->
<html>
<head>
<title>Title of new page</title>
</head>
<frameset cols="100%">
<frame src="http://www.mydomain.com/new-page.html">
<noframes>
<a href="http://www.mydomain.com/new-page.html">Link
to new page</a>
</noframes>
</frameset>
</html>
Advertising