CodexBloom - Programming Q&A Platform

Apache 2.4.54 - Redirect Loop with mod_rewrite When Using SSL

👀 Views: 24 đŸ’Ŧ Answers: 1 📅 Created: 2025-07-15
apache ssl mod_rewrite Apache

I need help solving Hey everyone, I'm running into an issue that's driving me crazy... Does anyone know how to I've been struggling with this for a few days now and could really use some help. I'm currently dealing with a redirect loop scenario in Apache 2.4.54 when trying to enforce SSL on my website. I have the following configuration in my virtual host file: ```apache <VirtualHost *:80> ServerName example.com RewriteEngine On RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] </VirtualHost> <VirtualHost *:443> ServerName example.com SSLEngine on SSLCertificateFile /path/to/certificate.crt SSLCertificateKeyFile /path/to/private.key DocumentRoot /var/www/html # Other SSL settings </VirtualHost> ``` After enabling this configuration, I find that when I access `http://example.com`, it redirects to `https://example.com` as expected, but then upon accessing `https://example.com`, it triggers another redirect back to `http://example.com`, resulting in a continuous loop. I've tried adding `RewriteCond` to check if the request is already using HTTPS: ```apache RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] ``` However, this hasn't resolved the scenario. I also checked my SSL configurations and ensured that the certificate is valid, yet the question continues. Any insights on why this redirect loop is happening or how I can resolve it? I'm using Ubuntu 20.04 and the default Apache installation. Thanks in advance for your help! I'm working on a API that needs to handle this. Has anyone else encountered this? I've been using Apache for about a year now. Thanks for any help you can provide! Is this even possible? What am I doing wrong?