所謂重定向,說白了就是把各種網絡請求重新指向另一個地址。做SEO時,301(永久重定向)常用於URL規範化與域名遷移。下面介紹在各主流環境下如何實現301重定向。
1、IIS下設置301
Internet信息服務管理器 -> 虛擬目錄 -> 重定向到URL,填入目標URL,並勾選「資源的永久重定向」。
2、ASP下的301轉向代碼
- <%@ Language=VBScript %>
- <%
- Response.Status=”301 Moved Permanently”
- Response.AddHeader “Location”, “https://www.sem.cm/2011/03/19/how-to-do-a-301-redirect/”
- %>
3、ASP.Net下的301轉向代碼
- <script runat=”server”>
- private void Page_Load(object sender, System.EventArgs e)
- {
- Response.Status = “301 Moved Permanently”;
- Response.AddHeader(”Location”,”https://www.sem.cm/2011/03/19/how-to-do-a-301-redirect/“);
- }
- </script>
- ?>
4、PHP下的301轉向代碼
- header(”HTTP/1.1 301 Moved Permanently”);
- header(”Location: https://www.sem.cm/2011/03/19/how-to-do-a-301-redirect/”);
- exit();
- ?>
5、CGI Perl下的301轉向代碼
- $q = new CGI;
- print $q->redirect(”https://www.sem.cm/”);
- ?>
6、JSP下的301轉向代碼
- <%
- response.setStatus(301);
- response.setHeader( “Location”, “https://www.sem.cm/” );
- response.setHeader( “Connection”, “close” );
- %>
- ?>
7、Apache下301轉向代碼
新建.htaccess文件,輸入下列內容(需開啟mod_rewrite):
1)將不帶WWW的域名轉向到帶WWW的域名:
- Options +FollowSymLinks
- RewriteEngine on
- RewriteCond %{HTTP_HOST} ^kuankuan.cn [NC]
- RewriteRule ^(.*)$ https://www.sem.cm/$1 [L,R=301]
2)重定向到新域名:
- Options +FollowSymLinks
- RewriteEngine on
- RewriteRule ^(.*)$ https://www.sem.cm/$1 [L,R=301]
3)使用正則進行301轉向,實現偽靜態:
- Options +FollowSymLinks
- RewriteEngine on
- RewriteRule ^news-(.+)\.html$ news.php?id=$1
將news.php?id=123這樣的地址轉向到news-123.html。
8、Apache下在vhosts.conf中配置301
為實現URL規範化,SEO通常把不帶WWW的域名轉向到帶WWW域名,vhosts.conf配置如下:
- <VirtualHost *:80>
- ServerName www.kuankuan.cn
- DocumentRoot /home/kuankuan
- </VirtualHost>
-
- <VirtualHost *:80>
- ServerName kuankuan.cn
- RedirectMatch permanent ^/(.*) https://www.sem.cm/$1
- </VirtualHost>
Apache下除以上兩種方法外,還有其他配置與可選參數,建議查閱官方文檔。
301轉向情況檢測
https://www.seoconsultants.com/tools/headers.asp
https://www.internetofficer.com/seo-tool/redirect-check/