如何将www域名重定向至根域名?Zeit Now CLI配置问题
Hey there, let's break down your questions and fix that redirect issue you're facing!
The approach depends on where you're managing your DNS and hosting, but here are the most common ways:
- DNS Provider Direct Setup: Most DNS services offer a "URL Redirect" or "Forwarding" feature. Just locate this option, select
www.yourdomain.com, set the redirect type to 301 Permanent, and point it toyourdomain.com. - Zeit Now/Vercel-Specific: Since you're working with Zeit Now, the cleaner way is to use its alias system to handle the redirect directly, which ties into your second question below.
You're right about the wildcard CNAME record (* CNAME alias.zeit.co) taking precedence—it matches all unspecified subdomains, including www, so your explicit www CNAME gets ignored. Let's fix this step by step:
Step 1: Resolve the DNS Conflict
First, you have two options here:
- Remove the wildcard CNAME (if you don't need it for other subdomains):
now dns remove mydomain.com * CNAME - Keep the wildcard record: Skip deleting it, but we'll use Zeit Now's alias redirect feature instead of DNS CNAME to make the www redirect work.
Step 2: Set Up the Root Domain Alias
Ensure your root domain is linked to your Zeit Now deployment first:
# Replace <your-deployment-id> with your actual deployment ID (find it via `now list`) now alias set <your-deployment-id> mydomain.com
If you haven't added your domain to Zeit Now yet, run now domains add mydomain.com first—it will auto-configure the necessary root domain DNS records (A/ALIAS) for you.
Step 3: Configure the www Redirect
Now set up the www alias with a permanent redirect to the root domain:
now alias set <your-deployment-id> www.mydomain.com --redirect mydomain.com --permanent
The --permanent flag ensures this is a 301 redirect, which is best for SEO and browser caching.
Step 4: Verify the Setup
Wait 1-5 minutes for DNS changes to propagate, then visit www.mydomain.com. You should be automatically redirected to mydomain.com, and checking your browser's network tab will show a 301 status code confirming the permanent redirect.
内容的提问来源于stack exchange,提问作者Joon

