You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

.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_rewrite module is enabled on your server. You can confirm this via your hosting control panel or run a2enmod rewrite if you have SSH access.
  • DNS record issue: Make sure your root domain example.com is set as an A record pointing to your server's IP address, not a CNAME to www.example.com. CNAME records for root domains can cause unexpected behavior with HTTP requests, especially if your server isn't configured to handle example.com as a valid host.

Quick recap

  • http://example.com → Will redirect correctly to https://www.example.com with the above config.
  • http://www.example.com → Will redirect correctly to https://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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:25:21