首页 > SEO技术 > 301 Redirect 永久重定向的实现

301 Redirect 永久重定向的实现

作者:highdiy 时间:2006年10月9日

在我们的网站建设中,时常会遇到需要网页重定向的情况:象网站调整,如改变网页目录结构,网页被移到一个新地址,再或者,网页扩展名改变,如因应用需要把.php改成.Html或.shtml,在这种情况下,如果不做重定向,则用户收藏夹或搜索引擎数据库中旧地址只能让访问客户还会得到一个404页面错误信息,访问流量白白丧失;再如某些注册了多个域名的网站,也需要通过重定向让访问这些域名的用户自动跳转到主站点,等等。

常用的重定向方式有: 301 redirect, 302 redirect 与 meta fresh:

  • 301 redirect: 301代表永久性转移(Permanently Moved),301重定向是网页更改地址后对搜索引擎友好的最好方法,只要不是暂时搬移的情况,都建议使用301来做转址。
  • 302 redirect: 302代表暂时性转移(Temporarily Moved ),在前些年,不少Black Hat SEO曾广泛应用这项技术作弊,目前,各大主要搜索引擎均加强了打击力度,象Google前些年对Business.com以及近来对BMW德国网站的惩罚。即使网站客观上不是spam,也很容易被搜寻引擎容易误判为spam而遭到惩罚。
  • meta fresh: 这在2000年前比较流行,不过现在已很少见。其具体是通过网页中的meta指令,在特定时间后重定向到新的网页,如果延迟的时间太短(约5秒之內),会被判断为spam。

这里我们主要谈谈如何通过301 Redirect实现网页重定向

IIS服务器实现301重定向

* 打开internet信息服务管理器,在欲重定向的网页或目录上按右键,见下图
* 选中“重定向到URL”
* 在对话框中输入目标页面的地址
* 切记,记得选中“资源的永久重定向”
* 当然,最后要点击“应用”

Apache服务器实现301重定向

相比较来说,Apache实现起来要比IIS简单多了。在Apache中,有个很重要的文件.htaccess,通过对它的设置,可以实现很多强大的功能,301重定向只是其中之一。

Redirect permanent / http://seo.highdiy.com/ ;将目录下内容重定向到http://seo.highdiy.com/
redirect permanent /old.html http://seo.highdiy.com/new-url/ ;将网页old.html内容重定向到http://seo.highdiy.com/new-url/

通过合理地配置重定向参数中的正则表达式,可以实现更复杂的匹配。有兴趣的朋友可参考Apache手册。

其他方法还有:

PHP下的301重定向

<? Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://seo.highdiy.com" );?>

ASP下的301重定向

<%@ Language=VBScript %>
<% Response.Status=”301 Moved Permanently” Response.AddHeader “Location”, ” http://seo.highdiy.com”>

ASP .NET下的301重定向

<script runat=”server”>
private void Page_Load(object sender, System.EventArgs e)
{
Response.Status = “301 Moved Permanently”;
Response.AddHeader(”Location”,”http://seo.highdiy.com”);
}
</script>

配置完成后,要认真检查一下是否正确。Internet有很多类似的Server Header检查工具,如Check Server Headers Tool - HTTP Status Codes Checker或本站提供的这个网页重定向检查工具。

作者: highdiy
原载: 点石互动搜索引擎优化博客
版权声明:本文已经获得作者授权发布,转载请保留本版权信息,严禁一切非法复制。

highdiy的其他文章:

SEO技术

  1. Simon
    2006年10月9日16:59 | #1

    帮你补充一个完整点的版本:

    301 Redirects & Canonical URL Issues Tutorial
    Registered ©2006 SEO Image, Inc

    When you move a file on your server you want to be certain that you pass the proper redirect directive to the browsers and more importantly, the Search Engines. The 301 redirect is the only redirect we recommend for anyone o use without advanced knowledge of using redirects. 302 redirects are actually used by SEO's for some specific reasons such as moving domain names from an old domain.com to a new domain.com

    301 Redirects & Canonical Redirects for Apache
    This is done on the invisible file named ".htaccess" this file is located in the root of your Web Server.
    For a single page redirect (NEVER use a 302 make sure it says either "Permanent" or "301" in the file if it doesn't it will pass a 302 by default:
    redirect 301 /old-file-name-html http://www.yourdomain.com/new-file.html
    or
    redirect permanent /old-file.html http://www.yourdomain.com/new-file.html
    Notice all that is needed is one space between the old path and the new URL. The new URL must contain an Absolute URL: meaning it uses http://www.yourdomain.com/yourfile.html. If there is a space in the file name use otherwise it will not work, you can only leave one space and that is between the old file path and the new url. Here is an example:
    /old file.html http://www.yourdomain.com/new-file.html
    Apache Server Canonical Redirect
    (non-www to www):
    This should always be done as some search engines do not properly understand the difference and may count it as a duplicate page, additionally if you us domain.com/index.html or domain.com index.php the Search Engine might get lost. Since Google is the most popular Search Engine, it also happens to be the least advanced with canonical issues and redirects. Google is also in a major anti-spam battle so it would actually hurt your rankings unintentionally as its a bit archaic in its handling of canonical issues and redirects. What Google will see as the same page duplicated on your site (In reality its all the same page).
    domain.com
    domain.com/
    http://www.domain.com/
    http://www.domain.com
    domain.com/index.html
    http://www.domain.com/index.html
    Since Google favors penalties over relative content you need to make sure you NEVER link to your home page as "index.html" or any other file extension. The automated factors of the Google algorithm would ban you without warning, and Google makes it a policy to not notify webmasters of penalties, so you could be spending years trying to resolve an issue like this. Make all your links Absolute to a least your home page so any link on your site should always link to "http://www.yourdomain.com/" be sure to use the trailing slash as some servers will pass a redirect to the trailing slash if you do not, and this will be a 302 redirect. This is a known issue on WebStar Server a Mac Platform.
    ALWAYS:

    NEVER:
    -Not direct to the domain.
    - No Trailing Slash
    Now for the .htaccess canonical redirect for the NON-www. to the www.
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^domain\.com
    RewriteRule (.*)
    http://www.domain.com/$1 [R=301,L]
    NOTE: Some Versions of Apace require an extra line or two of code for SymLinks and a different structure of the rewrite rule as follows:

    RewriteEngine On
    Options FollowSymlinks
    RewriteBase /
    RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
    A More advanced version will even allow you to 301 your IP address and any subdomains:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} .
    RewriteCond %{HTTP_HOST} !^www\.domain\.com
    RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
    The above code will 301 your non-www to your www, as well as redirect your IP address as a 301 to your www.
    To redirect your http pages but NOT your https pages use:
    Say for instance, your secure certificate was not created as http://www.domain.com, but was instead issued as domain.com then you would need to use the following and be sure to combine this with ABSOLUTE links (absolute links use the full URL), or you may unintentionally cause duplicate https and http paths.
    RewriteCond %{SERVER_PORT} ^80
    RewriteCond %{HTTP_HOST} ^domain\.com
    RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L,QSA]

    301 Redirects & Canonical Redirects:
    Microsoft IIS Web Servers
    To Perform this you need administrative access to the IIS Server Application. If you do not have this then you can do the redirects in ASP or PHP.
    Single Page Redirect:
    1. Open Internet Services Manager then "right-click" on the file or folder you want to redirect to a new page or folder.
    2. Select "redirection to a URL".
    3. Enter the URL where you want the redirect to go to.
    4. Check the fields: "exact url entered above" and "A permanent redirection for this resource".
    5. Click "Apply".
    PHP 301 Redirect
    This is added to the page that you want to redirect to a new page, it is triggered on the request for the specific page and the file will actually return the redirect.
    Single Page Redirect:

    PHP Canonical Redirect:
    (non-www to www)

    Redirects & Canonical Redirects for Active Server Pages (ASP)
    Single Page Redirect:
    This is placed on the page you are redirecting and is triggered when someone tries to access the page.

    ASP Canonical Redirect:
    (non-www to www):
    This code should be inserted into a global include file or any script which is executed for every page on the site before the page output begins. If your programmers are using ASP.net and/or they are not using a global file, you need to have them address this issue or perform the Canonical Redirect from the IIS Server Application (as above).

    301 Redirects for ASP.NET
    For Single Pages:

    private void Page_Load(object sender, System.EventArgs e)
    {
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location","http://www.domainname.com/new-page.aspx");
    }

    301 Redirects for Sun Web Servers
    In order to do this on a SUN platform you have to rewrite the SUN Web Server's standard action of always using a 302 redirect to always use a 301 redirect. You have to open the obj.conf configuration file and add the following lines below the line:

    Output fn="set-variable" error="301" noaction="true"

    The above will make the SUN web server to return 301 permanent redirect responses for all redirects. If you only want to use a 301 permanent redirect response for certain URLs or files, you can add following to the bottom of obj.conf:

    Output fn="set-variable" error="301" noaction="true"

  2. Simon
  3. lyc
    2006年10月9日17:34 | #3

    谢谢zac版:P

  4. 2006年10月10日09:39 | #4

    ASP下的301重定向

    <%@ Language=VBScript %>
    <% Response.Status=”301 Moved Permanently” Response.AddHeader “Location”, ” http://seo.highdiy.com”>

    把数据转移好多久可以删除掉原来的程序目录

  5. 2006年10月10日10:16 | #5

    感谢楼主 学习了

  6. 2006年10月10日12:53 | #6

    是不是做了302就会被搜索引擎认为是作弊。。。

  7. 2006年10月12日14:20 | #7

    感谢楼主

  8. 2007年4月20日13:30 | #8

    ASP下301重定向只要这样就行了吗?这样会不会被搜索引擎视为作避.
    <%@ Language=VBScript %>
    <% Response.Status=”301 Moved Permanently” Response.AddHeader “Location”, ” http://seo.highdiy.com”>

  9. 2007年6月3日18:36 | #9

    谢谢楼主,顶一下
    骑摩托http://www.7mt.cn

  10. 2007年6月9日09:41 | #10

    谢谢,不过好像虚拟主机,不能设置301吧?

  11. 2007年6月20日18:59 | #11

    如果有多个页面是不是每个叶面都要这样操作呢?

    重新定向的新的页面是可以变化的么?

    比如原来多个叶面都是动态的现在转化成静态的了,应该每个叶面都重新定向到新的静态页面吗? asp的

    谢谢

  12. 2007年6月24日15:41 | #12

    不错啊 呵呵

  13. 2007年6月27日18:13 | #13

    那个虚拟主机怎么设定301设定?

  14. 2007年8月20日03:38 | #14

    301重定向还是不太懂,,

  15. lgfifa
    2007年11月26日00:37 | #15

    想问个问题,网站进行全站静态化处理后,原有的动态路径都已改成静态化的.这该如何做301?忘指点

  16. 2008年2月11日09:53 | #16

    域名做了跳转是不是也可以做重定向呢?

  17. 2008年2月27日20:02 | #17

    虚拟主机的网站,对于生成了html后,无法实现301主机

  18. 2008年5月20日01:38 | #18

    301重定向是如何做的,我什么做都不行

  19. 海水焰
    2008年11月22日13:41 | #19

    如果不想实现永久定向这么麻烦的事情,还得在最初建立网站的时候考虑到链接的完善性。

  20. 转角遇到钱
    2008年12月7日17:16 | #20

    不是很详细!在IIS服务器下,全站301重定向,如何来做?

  21. 2008年12月14日16:26 | #21

    第一次见过这方面这么详细的资料,收藏了

  1. 2007年6月17日20:18 | #1
  2. 2007年10月17日11:33 | #2
  3. 2008年4月10日10:35 | #3
本文的评论功能被关闭了.