網站改版需將舊域名 liehuo.net 下某目錄整體 301 導向新域名 www.veryhuo.com,且原 URL 帶有查詢參數(如 liehuo_tag.asp?q=關鍵字)必須完整保留傳遞。
<%
If Request.ServerVariables("HTTP_HOST") = "liehuo.net" Then
Dim targetUrl, qs
qs = Request.ServerVariables("QUERY_STRING")
If qs <> "" Then
targetUrl = "https://www.veryhuo.com" & Request.ServerVariables("SCRIPT_NAME") & "?" & qs
Else
targetUrl = "https://www.veryhuo.com/"
End If
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", targetUrl
Response.End
End If
%>
| 變數 | 含義 | 範例 |
|---|---|---|
HTTP_HOST | 請求的主機名 | liehuo.net |
SCRIPT_NAME | 請求的腳本路徑 | /tag/liehuo_tag.asp |
QUERY_STRING | 查詢字串(不含 ?) | q=%C1%D2%BB%F0%CD%F8 |
redirect_301.asp。<!-- #include file="redirect_301.asp" -->。Response.Redirect(預設發送 302 臨時重導向)。若有大量舊頁面需導向,建議在 IIS 安裝 URL Rewrite Module 2.0,於 web.config 設定規則:
<rule name="OldDomainRedirect" stopProcessing="true">
<match url="^(.*)$" />
<conditions>
<add input="{HTTP_HOST}" pattern="^liehuo\.net$" />
</conditions>
<action type="Redirect" url="https://www.veryhuo.com/{R:1}" redirectType="Permanent" appendQueryString="true" />
</rule>
此法效能更佳、集中管理、不需修改 ASP 程式碼。