有些時候我們需要讓一個域名的訪問重定向到另一個域名,比如舊域名被牆的時候。
有些域名提供商會提供這項服務,但功能非常基礎,只能單純的匹配域名然後做跳轉,而且這種跳轉會丟失參數等信息,或者乾脆就匹配失敗,基本無法使用。
要重定向域名且保留請求參數,就需要你有一個專門的服務器來接受請求然後返回重定向信息。這個需求確實無法單純用 DNS 實現的。
使用一個服務器
由於我本身有博客服務器,而且我使用了 hestia 面板管理服務器,所以很輕鬆就可以創建一個空的網站,然後使用 apache 的 .htaccess的 來實現匹配跳轉。
你只需要在網站根目錄創建這個文件,然後在裡邊寫入:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#================================================================================ # APACHE .HTACCESS - 301 PERMANENT REDIRECT # # This configuration redirects all traffic from an old domain to a new domain, # preserving any subdomain and the full request URI (path and query string). # # INSTRUCTIONS: # 1. Replace "old-domain.com" with your actual old domain name. # 2. Replace "new-domain.com" with your actual new domain name. # 3. Place this .htaccess file in the document root directory of your old website. #================================================================================ # Enable the rewrite engine RewriteEngine On #-------------------------------------------------------------------------------- # Rule 1: Redirect requests that have a subdomain #-------------------------------------------------------------------------------- # This condition checks if the requested hostname consists of a subdomain part # followed by your old domain name (e.g., "www.old-domain.com" or "sub.old-domain.com"). # The subdomain part is captured for later use. RewriteCond %{HTTP_HOST} ^(.+)\.old-domain\.com$ [NC] # This rule constructs the new URL. # %1 is the subdomain captured from the RewriteCond above. # $1 is the entire request path and query string (e.g., "/page/index.html?id=123"). # The [R=301,L] flags specify a 301 Permanent Redirect and that this is the last rule to process. RewriteRule ^(.*)$ https://%1.new-domain.com/$1 [R=301,L] #-------------------------------------------------------------------------------- # Rule 2: Redirect requests for the apex/root domain (no subdomain) #-------------------------------------------------------------------------------- # This condition checks if the requested hostname is exactly the old apex domain. RewriteCond %{HTTP_HOST} ^old-domain\.com$ [NC] # This rule redirects to the new apex domain, preserving the request path and query string ($1). RewriteRule ^(.*)$ https://new-domain.com/$1 [R=301,L] |
在上面的例子中, 舊-域 和 新的-域 就是你的舊域名和新域名,對應修改即可(注意域名後綴也要對應,不要留著 com)。這樣,你只需要將你的域名解析到這個服務器,就可以實現跳轉了。同樣,你也可以給網站添加多個別名,這樣就可以將多個子域都解析到這裡,進行轉發。
使用 Cloudflare
如果你沒有方便的服務器進行域名停放,也可以考慮使用 cf 的免費服務。這是 Gemini 2.5 pro 生成的結果,我並沒有嘗試,但理論上也是可行的。潛在缺點有兩個:
- 解析不穩定,cf 在國內很多節點都被干擾和屏蔽了
- 遷移時間長,需要修改 ns 代理,生效時間可達 24 小時,這期間域名可能解析不了之類的,很難搞。因為你必須將域名放到 cf 才能操作
操作步驟:
- 註冊Cloudflare賬戶:如果你還沒有,可以免費註冊一個。
- 添加舊域名:在Cloudflare中添加你即將過期的舊域名。
- 更改NS記錄:Cloudflare會提供給你兩個新的Nameserver(NS)地址。你需要登錄到你舊域名的註冊商(如GoDaddy, Namecheap等),將該域名的NS記錄修改為Cloudflare提供的那兩個。這個過程通常需要幾小時到48小時生效。
- 設置跳轉規則:
- 在Cloudflare控制面板中,找到你的舊域名,然後進入 “規則” > “重定向規則” (這是較新的功能) 或者 “頁面規則” (舊功能,依然可用)。
- 創建一個規則:
- 如果傳入請求匹配… (當訪問的URL是): 舊-域.同/*
- 然後… (那麼執行):
- 類型: 動態的 (如果是Redirect Rules)
- URL重定向: HTTPS://new-domain.com{http.request.uri.path} (這是Redirect Rules的語法)
- 或者 轉發網址 (如果是Page Rules): HTTPS://new-domain.com/jul1
- 狀態代碼: 301 永恆的 重定向
這裡的 * 和 {HTTP.要求.Uri.path} 或 $1 是關鍵。它們是通配符和變量,代表了域名後面所有的路徑和參數,確保了請求可以被原封不動地傳遞過去。
本文由 落格博客 原創撰寫:落格博客 » 如何給舊域名做重定向
轉載請保留出處和原文鏈接:https://www.logcg.com/archives/3896.html