10-03
12

301和302重定向 各类语言代码大全

最近好像陷进重定向的问题里爬不出来了,先说说301和302重定向好了,301是永久转移,就是说原来的页面改名字了,永远用这个地址代替了。302就是“找到”(Found)也就是临时重定向。


先说说html的302重定向,不是为了page rank,不是为了任何特别的问题,仅仅因为我把一些人家的一些文件名仿1.htm改成了service.html,为了不要让在google查到01.htm的人看不到网页,只能做重定向。而要命的是他们用的是IIS server,而且服务商的服务奇差无比,我不指望他们能帮着添加301重定向。最后在clsc.net找到了《Hijack: The 302 Exploit, Redirects and Google》。其他啥都没有看,就抄了一句话:

<meta http-equiv="refresh" content="0;url=http://www.target-website.com/folder/file.html">


其实是一篇可以好好看看得文章。

接着说说301,全文引用带部分翻译:webconfs.com的《How to Redirect a Web Page》

301重定向是最有效率的并且对搜索引擎友好的重定向方式。设置并不困难,而且能保留搜索引擎的评分,如果你需要重命名或者移动页面,那么这是安全的选择。“301”是“永久转移”的意思。

301 redirect is the most efficient and Search Engine Friendly method for webpage redirection. It's not that hard to implement and it should preserve your search engine rankings for that particular page. If you have to change file names or move pages around, it's the safest option. The code "301" is interpreted as "moved permanently".

可以用搜索引擎友好度测试来检验你的重定向。

You can Test your redirection with Search Engine Friendly Redirect Checker

下面是一些实现URL重定向的方法

Below are a Couple of methods to implement URL Redirection

IIS Redirect
In internet services manager, right click on the file or folder you wish to redirect
Select the radio titled "a redirection to a URL"
Enter the redirection page
Check "The exact url entered above" and the "A permanent redirection for this resource"
Click on 'Apply'


ColdFusion Redirect
<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.new-url.com">


PHP Redirect
<?
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>


ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>



ASP .NET Redirect
<script 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>

JSP (Java) Redirect
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-url.com/" );
response.setHeader( "Connection", "close" );
%>

CGI PERL Redirect
$q = new CGI;
print $q->redirect("http://www.new-url.com/");
Ruby on Rails Redirect
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end


Redirect Old domain to New domain (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all your directories and pages of your old domain will get correctly redirected to your new domain.

The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]


Please REPLACE www.newdomain.com in the above code with your actual domain name.

In addition to the redirect I would suggest that you contact every backlinking site to modify their backlink to point to your new website.

Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

Redirect to www (htaccess redirect)

Create a .htaccess file with the below code, it will ensure that all requests coming in to domain.com will get redirected to www.domain.com

The .htaccess file needs to be placed in the root directory of your old website (i.e the same directory where your index file is placed)

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [nc]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,nc]
Please REPLACE domain.com and www.newdomain.com with your actual domain name


Note* This .htaccess method of redirection works ONLY on Linux servers having the Apache Mod-Rewrite moduled enabled.

How to Redirect HTML
Please refer to section titled 'How to Redirect with htaccess', if your site is hosted on a Linux Server and 'IIS Redirect', if your site is hosted on a Windows Server.



[本日志由 blurxx 于 2010-04-10 07:06 PM 编辑]
文章来自: 本站原创
引用通告: 查看所有引用 | 我要引用此文章
Tags: 301 重定向 302
相关日志:
评论: 0 | 引用: 0 | 查看次数: -
发表评论
昵 称:
密 码: 游客发言不需要密码.
内 容:
验证码: 验证码
选 项:
虽然发表评论不用注册,但是为了保护您的发言权,建议您注册帐号.