.htaccess重定向失效:将example.com重定向至https://www.example.com
Got it, let's work through your redirect issue with all the constraints you've laid out—root domain without SSL, CMS-managed www with its own SSL, and .htaccess not playing nice so far. Here's what you need to know and fix:
First, the unavoidable constraint
Since your root domain example.com doesn't have an SSL certificate, any attempt to visit https://example.com will trigger a browser SSL error before the request even reaches your .htaccess file. That's because the SSL handshake happens before Apache processes rewrite rules. There's no way around this with .htaccess alone—if you want to eliminate that warning, you'll need to add an SSL certificate to example.com (like a free Let's Encrypt cert) or check if your CMS can extend its SSL coverage to the root domain.
Fixing the redirects that can work (all HTTP traffic)
The good news is we can ensure all HTTP requests (both http://example.com and http://www.example.com) get redirected to https://www.example.com properly. The critical detail here is placing your redirect rules before your CMS's existing .htaccess rules—most CMSes (like WordPress, Drupal) have their own rewrite blocks that will override your rules if yours come after.
Here's the exact .htaccess config you should use:
# Enable rewrite engine RewriteEngine On # Redirect non-www HTTP to HTTPS www RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] # Redirect www HTTP to HTTPS www RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] # Optional: Catch any other subdomains (if you need this) RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC] RewriteCond %{HTTPS} off RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301] # --- Paste your CMS's existing .htaccess rules BELOW this line ---
Why your previous .htaccess might have failed
- Rule order: If you added your redirects after the CMS's rewrite rules, the CMS's routing rules (like sending all requests to index.php) would trigger first, and your redirects never ran. Always put redirects at the top of the file.
- mod_rewrite not enabled: Double-check that Apache's
mod_rewritemodule is enabled on your server. You can confirm this via your hosting control panel or runa2enmod rewriteif you have SSH access. - DNS record issue: Make sure your root domain
example.comis set as an A record pointing to your server's IP address, not a CNAME towww.example.com. CNAME records for root domains can cause unexpected behavior with HTTP requests, especially if your server isn't configured to handleexample.comas a valid host.
Quick recap
http://example.com→ Will redirect correctly tohttps://www.example.comwith the above config.http://www.example.com→ Will redirect correctly tohttps://www.example.com.https://example.com→ Will show an SSL warning (only fixable by adding an SSL cert to the root domain).
内容的提问来源于stack exchange,提问作者Aditya

