有些时候我们需要让一个域名的访问重定向到另一个域名,比如旧域名被墙的时候。
有些域名提供商会提供这项服务,但功能非常基础,只能单纯的匹配域名然后做跳转,而且这种跳转会丢失参数等信息,或者干脆就匹配失败,基本无法使用。
要重定向域名且保留请求参数,就需要你有一个专门的服务器来接受请求然后返回重定向信息。这个需求确实无法单纯用 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] |
在上面的例子中, old-domain 和 new-domain 就是你的旧域名和新域名,对应修改即可(注意域名后缀也要对应,不要留着 com)。这样,你只需要将你的域名解析到这个服务器,就可以实现跳转了。同样,你也可以给网站添加多个别名,这样就可以将多个子域都解析到这里,进行转发。
使用 Cloudflare
如果你没有方便的服务器进行域名停放,也可以考虑使用 cf 的免费服务。这是 Gemini 2.5 pro 生成的结果,我并没有尝试,但理论上也是可行的。潜在缺点有两个:
- 解析不稳定,cf 在国内很多节点都被干扰和屏蔽了
- 迁移时间长,需要修改 ns 代理,生效时间可达 24 小时,这期间域名可能解析不了之类的,很难搞。因为你必须将域名放到 cf 才能操作
操作步骤:
- 注册Cloudflare账户:如果你还没有,可以免费注册一个。
- 添加旧域名:在Cloudflare中添加你即将过期的旧域名。
- 更改NS记录:Cloudflare会提供给你两个新的Nameserver(NS)地址。你需要登录到你旧域名的注册商(如GoDaddy, Namecheap等),将该域名的NS记录修改为Cloudflare提供的那两个。这个过程通常需要几小时到48小时生效。
- 设置跳转规则:
- 在Cloudflare控制面板中,找到你的旧域名,然后进入 “Rules” > “Redirect Rules” (这是较新的功能) 或者 “Page Rules” (旧功能,依然可用)。
- 创建一个规则:
- If incoming requests match… (当访问的URL是): old-domain.com/*
- Then… (那么执行):
- Type: Dynamic (如果是Redirect Rules)
- URL redirect: https://new-domain.com{http.request.uri.path} (这是Redirect Rules的语法)
- 或者 Forwarding URL (如果是Page Rules): https://new-domain.com/$1
- Status code: 301 Permanent Redirect
这里的 * 和 {http.request.uri.path} 或 $1 是关键。它们是通配符和变量,代表了域名后面所有的路径和参数,确保了请求可以被原封不动地传递过去。
本文由 落格博客 原创撰写:落格博客 » 如何给旧域名做重定向
转载请保留出处和原文链接:https://www.logcg.com/archives/3896.html