12种实现301网页重定向方法的代码实例(含Web编程语言和Web服务器)
为什么需要使用301重定向:
1.保留搜索引擎的排名:301重定向是最有效的方法,不会影响到搜索引擎对页面的排名。
2.保留访客和流量:如果你将页面链接到大量方法可以访问过的地址,如果不是用重定向的话你就会失去这些用户(不解)原文:Ifyoumoveyourpopularpagetowhichalotofvisitorshavealreadylinked,youmaylosethemifyoudon'tusedredirectmethod.Thisprovidesagreatwaytoprovideyourvisitorswiththeinformationtheywerelookingforandpreventyoufromlosingyourtraffic.
下面是11中实现301重定向的方法:
1.HTML重定向/Meta刷新
将下面HTML重定向代码放在网页的<head>节点内:
<metaHTTP-EQUIV="REFRESH"content="0;url=http://www.yourdomain.com">
上述代码立即将访客重定向到另外一个页面,你可以修改content中的0这个值来表示几秒钟后才进行重定向。例如content="3;url=http://www.oschina.net/"表示三秒后再重定向。
2.PHP重定向
<? Header("HTTP/1.1301MovedPermanently"); Header("Location:http://www.new-url.com"); ?>
3.ASPRedirect
<%@Language=VBScript%> <% Response.Status="301MovedPermanently" Response.AddHeader"Location","http://www.new-url.com/" %>
4.ASP.NETRedirect
<scriptrunat="server"> privatevoidPage_Load(objectsender,System.EventArgse) { Response.Status="301MovedPermanently"; Response.AddHeader("Location","http://www.new-url.com"); } </script>
5.JSPRedirect
<% response.setStatus(301); response.setHeader("Location","http://www.new-url.com/"); response.setHeader("Connection","close"); %>
6.CGIPERLRedirect
$q=newCGI; print$q->redirect("http://www.new-url.com/");
7.RubyonRailsRedirect
defold_action headers["Status"]="301MovedPermanently" redirect_to"http://www.new-url.com/" end
8.ColdFusionRedirect
<.cfheaderstatuscode="301"statustext="Movedpermanently"> <.cfheadername="Location"value="http://www.new-url.com">
9.JavascriptURLRedirect
<head> <scripttype="text/javascript"> window.location.href='http://www.newdomain.com/'; </script> </head>
10.IISRedirect
在Internet服务管理器中右击你想要重定向的文件和文件夹,选择"aredirectiontoaURL".
然后输入目标网址,选中"Theexacturlenteredabove"和"Apermanentredirectionforthisresource"然后点击'Apply'按钮。
11.使用.htaccess进行重定向
创建一个.htaccess文件(代码如下)用来将访问呢domain.com重定向到www.domain.com下,该文件必须放置在网站的root目录,也就是首页放置的目录。
Options+FollowSymlinks RewriteEngineon rewritecond%{http_host}^domain.com[nc] rewriterule^(.*)$http://www.domain.com/$1[r=301,nc]
注意:.htaccess方法的重定向只能在Linux下使用Apache的mod_rewrite模块启用的情况下使用。
12.Nginx中的rewrite
server{ server_namewww.nhooo.comjb51.net; if($host='jb51.net'){ rewrite^/(.*)$https://www.nhooo.com/$1permanent; }