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

Apache配置SSL后出现Too many redirects错误求助

Fixing "Too many redirects" Loop with Apache & Certbot

Let's break down why you're hitting this redirect loop and fix it step by step:

1. First, Check for .htaccess Conflicts

Your www.website.com HTTPS VirtualHost has AllowOverride All enabled, which means any redirect rules in /var/www/html/.htaccess can clash with your VirtualHost-level redirects. For example, if your .htaccess has a rule that redirects www.website.com back to website.com, that creates an infinite loop.

  • Open /var/www/html/.htaccess and look for lines like:
    RewriteCond %{HTTP_HOST} ^www\.website\.com$ [NC]
    RewriteRule ^(.*)$ https://website.com/$1 [R=301,L]
    
  • If you find such rules, either comment them out (add # at the start) or delete them entirely.

2. Simplify & Clean Up Your VirtualHost Configs

Your current config works but can be streamlined to avoid ambiguity. Here's the optimized version:

Non-HTTPS (Port 80) Config

Combine both domain entries into one VirtualHost to handle all HTTP traffic in a single rule:

<VirtualHost *:80>
 ServerAdmin contact@website.com
 ServerName website.com
 ServerAlias www.website.com
 Redirect permanent / https://www.website.com/
</VirtualHost>

HTTPS (Port 443) Config

Keep the redirect for website.com to www.website.com, and ensure the www entry points correctly to your document root:

<IfModule mod_ssl.c>
 <VirtualHost *:443>
 ServerAdmin contact@website.com
 ServerName website.com
 Redirect permanent / https://www.website.com/
 SSLCertificateFile /etc/letsencrypt/live/website.com/fullchain.pem
 SSLCertificateKeyFile /etc/letsencrypt/live/website.com/privkey.pem
 Include /etc/letsencrypt/options-ssl-apache.conf
 </VirtualHost>

 <VirtualHost *:443>
 ServerAdmin contact@website.com
 ServerName www.website.com
 DocumentRoot /var/www/html
 <Directory /var/www/html>
 Options FollowSymLinks MultiViews
 AllowOverride All
 Order allow,deny
 allow from all
 </Directory>
 SSLCertificateFile /etc/letsencrypt/live/website.com/fullchain.pem
 SSLCertificateKeyFile /etc/letsencrypt/live/website.com/privkey.pem
 Include /etc/letsencrypt/options-ssl-apache.conf
 </VirtualHost>
</IfModule>

3. Verify Your SSL Certificate Covers Both Domains

A common hidden cause of redirect loops is when your SSL certificate only covers website.com (not www.website.com). When browsers try to load https://www.website.com, they hit a certificate error, which can trigger unexpected redirect behavior.

  • Check your certificate's domain list with this command:
    certbot certificates
    
  • Look for a line like Domains: website.com www.website.com. If www.website.com is missing, reissue the certificate to include both domains:
    certbot certonly --apache -d website.com -d www.website.com
    

4. Test & Restart Apache

Always validate your config before restarting to avoid downtime:

sudo apache2ctl configtest

If you see Syntax OK, restart Apache to apply changes:

sudo systemctl restart apache2

After these steps, all HTTP requests (with or without www) should redirect to https://www.website.com, and HTTPS requests to website.com will also land on the www version without loops.


内容的提问来源于stack exchange,提问作者Zemou

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:33:20