Sometimes we need to redirect access to another domain name,For example, when the old domain name is walled。
Some domain name providers provide this service,But the functions are very basic,Only simply match the domain name and then jump,And this jump will lose parameters and other information,Or simply fail to match,Basically unusable。
To redirect the domain name and retain the request parameters,You need to have a dedicated server to accept the request and return the redirect information。This requirement cannot be achieved simply by DNS。
Using a server
Since I have a blog server,And I used it hestia panelManage server,So it's easy to create an empty website,Then use apache .htaccess To achieve matching jump。
You just need to create this file in the website root directory,Then write in it:
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] |
In the above example, old-domain and new-domain It's your old domain name and new domain name,Just modify it accordingly (note that the domain name suffix must also correspond to it,Don't keep com)。Such,You just need to resolve your domain name to this server,You can jump。equally,You can also add multiple alias to the website,This allows you to parse multiple subdomains here,Forward。
Using Cloudflare
If you don't have a convenient server for domain name parking,You can also consider using cf's free service。This is Gemini 2.5 The result generated by pro,I didn't try,But it is also feasible in theory。There are two potential drawbacks:
- Unstable parsing,cf Many nodes in the country are interfered and blocked
- Long migration time,Need to modify the ns proxy,The effective time can be reached 24 hour,During this period, the domain name may not be resolved.,It's hard to do。Because you have to put the domain name into cf to operate
Operation steps:
- Register a Cloudflare account:If you haven't,You can register for one for free。
- Add old domain name:Add your old domain name that is about to expire in Cloudflare。
- Change NS records:Cloudflare will provide you with two new Nameserver (NS) addresses。You need to log in to youOld domain nameregistrars (such as GoDaddy, Namecheap, etc.),Modify the NS record of this domain name to the two provided by Cloudflare。This process usually takes several hours to 48 hours to take effect。
- Set jump rules:
- In Cloudflare Control Panel,Find your old domain name,Then enter "Rules” > “Redirect Rules” (This is a newer feature) or "Page Rules” (Old features,Still available)。
- Create a rule:
- If incoming requests match… (When the URL to be accessed is): old-domain.with/*
- Then… (Then execute):
- Type: Dynamic (If it is Redirect Rules)
- URL redirect: https://new-domain.com{http.request.uri.path} (This is the syntax of Redirect Rules)
- or Forwarding URL (If it is Page Rules): https://new-domain.com/$1
- Status code: 301 Permanent Redirect
here * and {http.request.uri.path} or $1 Is the key。They are wildcards and variables,Represents all paths and parameters behind the domain name,Ensure that the request can be passed intact。
Original article written by LogStudio:R0uter's Blog » How to redirect old domain names
Reproduced Please keep the source and description link:https://www.logcg.com/archives/3896.html