非黑白的生活

用黑白的眼睛闪出七彩的生活!

« 祖国啊,我爱您爱到打砸抢李白和日本、家乐福的纠葛 »

How to setup a 301 Redirect

The “301 Permanent Redirect” is the most efficient and search engine friendly method for redirecting websites. You can use it in several situations, including:

  • to redirect an old website to a new address
  • to setup several domains pointing to one website
  • to enforce only one version of your website (www. or no-www)
  • to harmonize a URL structure change

There are several ways to setup a 301 Redirect, below I will cover the most used ones:

PHP Single Page Redirect

In order to redirect a static page to a new address simply enter the code below inside the index.php file.

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

PHP Canonical Redirect

The Canonical 301 Redirect will add (or remove) the www. prefixes to all the pages inside your domain. The code below redirects the visitors of the http://domain.com version to 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']);
}
?>

Apache .htaccess Singe Page Redirect

In order to use this method you will need to create a file named .htaccess (not supported by Windows-based hosting) and place it on the root directory of your website, then just add the code below to the file.

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

Apache .htaccess Canonical Redirect

Follow the same steps as before but insert the code below instead (it will redirect all the visitors accessing http://domain.com to http://www.domain.com)

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

ASP Single Page Redirect

This redirect method is used with the Active Server Pages platform.

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

ASP Canonical Redirect

The Canonical Redirect with ASP must be located in a script that is executed in every page on the server before the page content starts.

<%
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
%>

 origin: DailyBlogTips

本站所有转载文章,其版权一律归原著者所有,与之相关的权益归原发表文章的报刊和网站,本人转载仅供学习、参考和方便阅读,没有其它任何从事商业的动机。任何个人或单位,如果不愿其作品被转载于此,敬请说明和通知。

本站所有原创作品均遵循 『 创作共用 Creative Commons 』协议
您可以随意转载我博客里的文章,但非我允许不得用于商业用途,转载的时候请在醒目的位置注明原文的出处并附上链接
本文链接地址:http://www.palfans.net/blog/post/2008/04/how_to_setup_a_301_redirect.htm

如果这是您第一次访问本站,欢迎您点击,及时得到本站最近的更新。

  • 相关文章:

发表评论:

[邮箱被保密,并能收到给您的留言回复。如已关联Gravatar,会同步您的头像]

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

日历

最新评论及回复

最近发表

Powered By Z-Blog 1.8 Spirit Build 80722 Code detection by Codefense

Copyright 2005-2008© www.palfans.net. Some Rights Reserved.  渝ICP备05012425号