如何将HTTPS非WWW域名重定向至HTTPS WWW域名?
Hey Rachel, totally get needing to redirect your root domain to the www version—this is such a common task, and I’ve got you covered with the most reliable methods depending on your server setup. Let’s dive in:
If your site runs on Apache, this is the go-to method. Head to your website’s root directory, create or edit the .htaccess file, and add this code:
RewriteEngine On RewriteCond %{HTTP_HOST} ^example\.com [NC] RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
R=301sets a permanent redirect, which is best for SEO since it tells search engines to update their indexes to the www version.NCmakes the rule case-insensitive (so it works forExample.comtoo).Ltells Apache this is the last rule to process once matched.
For Nginx, locate your site’s configuration file (usually in /etc/nginx/sites-available/ or /etc/nginx/conf.d/). Update or add these server blocks:
# Redirect root domain to www server { listen 80; listen [::]:80; server_name example.com; return 301 https://www.example.com$request_uri; } # Your main www site configuration (make sure SSL is set up here) server { listen 443 ssl; listen [::]:443 ssl; server_name www.example.com; # Add your SSL certificate paths, root directory, and other site config here }
After making changes, restart Nginx to apply them:
sudo systemctl restart nginx
If you can’t modify server config right now, add this to the <head> section of your original site’s HTML as a temporary workaround:
<meta http-equiv="refresh" content="0; URL=https://www.example.com">
⚠️ Note: This is less ideal for SEO than server-level redirects, so switch to one of the first two methods as soon as you can.
- Make sure
www.example.comhas a valid SSL certificate (no "not secure" warnings after redirect). - Test the redirect by visiting
https://example.com—you should land on the www version immediately. - Use your browser’s DevTools (Network tab) to confirm the redirect status code is 301.
内容的提问来源于stack exchange,提问作者Rachel Willy

