Home » Wordpress 技巧

如何设定 WordPress 博客的301重定向?

“301 永久重定向”是一种对搜索引擎最友好的一种重定向方式。 你有可能会在下面几种情况下用到它:

  • 把你旧的博客定向到新的网址。
  • 使你博客的网址只有一种访问方式(有www或者没有www)
  • 使你的URL结构修改后保持一致。

有很多中设定301重定向的方法,下面我将就几种常用的来说明一下:

PHP 单页面重定向

你只要在 index.php 文件中加入一下代码就可以把一个静态的网页重定向到一个新的网址:

<?php
header("HTTP/1.1 301 Moved Permanently");
header("Location: http://www.newdomain.com/page.html");
exit();
?>

PHP 典型重定向

典型301重定向将会给你域名下的所有网页都添加 www. 前缀。下面的代码会把你博客的访问者 http://domain.com 定向到 http://www.domain.com 。

<?php
if (substr($_SERVER['HTTP_HOST'],0,3) != ‘www’) {
header(’HTTP/1.1 301 Moved Permanently’);
header(’Location: http://www.’.$_SERVER['HTTP_HOST']
.$_SERVER['REQUEST_URI']);
}
?>

利用 .htaccess 文件进行单页重定向

为了能够使用到这种方法,你必须要在你网站的根目录下建立一个.htaccess文件 (该文件在 Windows-based 主机中不支持) ,然后在你的.htaccess文件加入如下代码:

Redirect 301 /old/oldpage.htm /new/http://www.domain.com/newpage.htm

.htaccess 文件典型重定向

就像上一步那样添加.htaccess文件,不过添加的代码应该如下 (这些代码将会使所有访问 http://domain.com 的都定向到 http://www.domain.com):

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]

ASP 单页面重定向

这种重定向方法使用与ASP(Active Server Pages)平台。

<%
Response.Status="301 Moved Permanently"
Response.AddHeader='Location','http://www.new-url.com/'
%>

ASP 典型重定向

ASP 典型重定向必须位于服务器执行每一页的脚本那里,并应在每页的内容开始之前:

<%
If InStr(Request.ServerVariables("SERVER_NAME"),"www") = 0 Then
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www."
& Request.ServerVariables("HTTP_HOST")
& Request.ServerVariables("SCRIPT_NAME")
End if
%>

原文:How to setup a 301 Redirect

译文:如何设定 WordPress 博客的301重定向?

Popularity: unranked [?]

Share/Bookmark this!

Leave a reply

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

Side Notes

This entry was posted by 莫 涯 on December 9, 2008 at 12:15 am and filed under Wordpress 技巧 category.

You can add your comments or trackback from your own site. To keep you updated to the latest discussion, you can subscribe to these comments via RSS.

This is a Gravatar-enabled weblog. To get your own globally recognized avatar, please register at Gravatar.

最新文章

最新评论

Tag Cloud

301 Redirect 301重定向 2010 about All in one seo pack AutosCraze blog CSS Darren feed FreshMag Global Translator Google GreenFun HTML Insider name.com Problogger robots.txt文件 RSS Feed seo Wordpress Wordpress 主题 Wordpress主题 代码 优惠码 关于 博客 博客建设 域名 工具 建设 技巧 提交 插件 文章 杂志型主题 标签 注册 绿色 网赚 评论 选择 链接 页面