寻求可行的301重定向方案:HTTP转HTTPS、非www转www
301 Redirect Configuration to Force HTTPS + www
Got it, let's get you sorted with the exact 301 redirect rules you need — all variants of your domain will point to https://www.domain.tld/ as required. Below are configurations for the two most common web servers:
Apache (using .htaccess or httpd.conf)
If you're running Apache, add these rules to your root .htaccess file (or your server's virtual host config):
# Enable rewrite engine RewriteEngine On # Redirect HTTP (both bare and www) to HTTPS + www RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L] # Redirect HTTPS bare domain to HTTPS + www RewriteCond %{HTTPS} on RewriteCond %{HTTP_HOST} ^domain.tld$ [NC] RewriteRule ^(.*)$ https://www.domain.tld/$1 [R=301,L]
RewriteEngine Onturns on Apache's URL rewriting module (make sure this module is enabled on your server first).- The first rule catches any HTTP request (whether it's
http://domain.tldorhttp://www.domain.tld) and redirects it to the HTTPS + www version. - The second rule handles the case where someone accesses the HTTPS bare domain (
https://domain.tld) and sends them to the www variant. - Don't forget to replace
domain.tldwith your actual domain name!
Nginx
For Nginx, add these rules to your server block configuration (usually found in /etc/nginx/sites-available/ or similar):
# Redirect HTTP bare domain to HTTPS + www server { listen 80; server_name domain.tld; return 301 https://www.domain.tld$request_uri; } # Redirect HTTP www domain to HTTPS + www server { listen 80; server_name www.domain.tld; return 301 https://www.domain.tld$request_uri; } # Redirect HTTPS bare domain to HTTPS + www server { listen 443 ssl; server_name domain.tld; # Include your SSL certificate config here (ssl_certificate, ssl_certificate_key) return 301 https://www.domain.tld$request_uri; } # Your main HTTPS + www server block server { listen 443 ssl; server_name www.domain.tld; # Add your SSL config, root directory, index files, and other site settings here }
- Each
serverblock targets a specific domain variant and sends a 301 redirect to the desiredhttps://www.domain.tldURL. - Make sure your SSL certificates cover both
domain.tldandwww.domain.tld(a wildcard cert or SAN cert works here).
Quick Test Tip
After setting up the rules, you can verify they work using curl in your terminal:
curl -I http://domain.tld curl -I http://www.domain.tld curl -I https://domain.tld
Each should return a 301 Moved Permanently status code pointing to https://www.domain.tld/.
内容的提问来源于stack exchange,提问作者J-ZA
相关产品推荐
相关产品推荐

