Apache配置SSL后出现Too many redirects错误求助
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/.htaccessand 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. Ifwww.website.comis 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

