12種實現(xiàn)301網(wǎng)頁重定向方法的代碼實例(含Web編程語言和Web服務(wù)器)
為什么需要使用301重定向:
1. 保留搜索引擎的排名: 301 重定向是最有效的方法,不會影響到搜索引擎對頁面的排名。
2. 保留訪客和流量: 如果你將頁面鏈接到大量方法可以訪問過的地址,如果不是用重定向的話你就會失去這些用戶(不解)原文:If you move your popular page to which a lot of visitors have already linked, you may lose them if you don't used redirect method. This provides a great way to provide your visitors with the information they were looking for and prevent you from losing your traffic.
下面是 11 中實現(xiàn) 301 重定向的方法:
1. HTML 重定向/Meta 刷新
將下面 HTML 重定向代碼放在網(wǎng)頁的 <head> 節(jié)點內(nèi):
<meta HTTP-EQUIV="REFRESH" content="0; url=http://www.yourdomain.com">
上述代碼立即將訪客重定向到另外一個頁面,你可以修改 content 中 的 0 這個值來表示幾秒鐘后才進行重定向。例如 content="3; url=http://www.oschina.net/" 表示三秒后再重定向。
2. PHP 重定向
Header( "HTTP/1.1 301 Moved Permanently" );
Header( "Location: http://www.new-url.com" );
?>
3. ASP Redirect
<%@ Language=VBScript %>
<%
Response.Status="301 Moved Permanently"
Response.AddHeader "Location","http://www.new-url.com/"
%>
4. 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>
5. JSP Redirect
<%
response.setStatus(301);
response.setHeader( "Location", "http://www.new-url.com/" );
response.setHeader( "Connection", "close" );
%>
6. CGI PERL Redirect
$q = new CGI;
print $q->redirect("http://www.new-url.com/");
7. Ruby on Rails Redirect
def old_action
headers["Status"] = "301 Moved Permanently"
redirect_to "http://www.new-url.com/"
end
8. ColdFusion Redirect
<.cfheader statuscode="301" statustext="Moved permanently">
<.cfheader name="Location" value="http://www.new-url.com">
9. Javascript URL Redirect
<head>
<script type="text/javascript">
window.location.;
</script>
</head>
10. IIS Redirect
在 Internet 服務(wù)管理器中右擊你想要重定向的文件和文件夾,選擇 "a redirection to a URL".
然后輸入目標網(wǎng)址,選中 "The exact url entered above" 和 "A permanent redirection for this resource" 然后點擊 'Apply' 按鈕。
11. 使用 .htaccess 進行重定向
創(chuàng)建一個 .htaccess 文件(代碼如下)用來將訪問呢 domain.com 重定向到 www.domain.com 下,該文件必須放置在網(wǎng)站的root目錄,也就是首頁放置的目錄。
RewriteEngine on
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_name www.dbjr.com.cn jb51.net;
if ($host = 'jb51.net' ) {
rewrite ^/(.*)$ http://www.dbjr.com.cn/$1 permanent;
}
相關(guān)文章
HTTP協(xié)議簡介_動力節(jié)點Java學(xué)院整理
這篇文章主要介紹了HTTP協(xié)議簡介,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07IDEA中使用Git拉取代碼時報 Git pull failed原因及解決方法
這篇文章主要介紹了IDEA中使用Git拉取代碼時報 Git pull failed原因及解決方法,本文給大家介紹的非常詳細對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08php/asp/asp.net中判斷百度移動和PC蜘蛛的實現(xiàn)代碼
這篇文章主要介紹了php/asp/asp.net中判斷百度移動和PC蜘蛛的實現(xiàn)代碼,需要的朋友可以參考下2015-09-09