CodexBloom - Programming Q&A Platform

Apache 2.4 Virtual Host Configuration for SSL Not Redirecting HTTP to HTTPS

πŸ‘€ Views: 62 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-04
apache http https ssl virtualhost

I keep running into I'm writing unit tests and I've searched everywhere and can't find a clear answer... I'm having trouble configuring my Apache 2.4 server to properly redirect HTTP traffic to HTTPS for a virtual host. I have the SSL certificates set up, and the HTTPS virtual host is working, but the redirection from HTTP isn't functioning as expected. Here's the configuration I've tried for my virtual hosts: ```apache <VirtualHost *:80> ServerName example.com ServerAlias www.example.com Redirect permanent / https://example.com/ </VirtualHost> <VirtualHost *:443> ServerName example.com ServerAlias www.example.com SSLEngine on SSLCertificateFile /path/to/your/certificate.crt SSLCertificateKeyFile /path/to/your/private.key SSLCertificateChainFile /path/to/your/chainfile.pem DocumentRoot /var/www/example </VirtualHost> ``` Despite this configuration, when I navigate to `http://example.com`, the page does not redirect to `https://example.com`. Instead, I receive a `403 Forbidden` error. I've checked the Apache error logs, and they show: ``` [error] [client 192.0.2.1] Directory index forbidden by Options directive: /var/www/example/ ``` I also verified that the `mod_rewrite` module is enabled. I tried adding the following lines to the `<VirtualHost *:80>` block, but it didn't help: ```apache RewriteEngine On RewriteCond %{HTTPS} off RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301] ``` I double-checked file permissions and ensured that the DocumentRoot exists and is accessible. Additionally, I've looked into the `AllowOverride` directive but haven’t made any changes there. Could anyone provide insight into why the HTTP to HTTPS redirection is not functioning? Are there any common pitfalls in the configuration that I might be missing? This is part of a larger application I'm building. Any help would be greatly appreciated! This is part of a larger REST API I'm building. I'm working with Apache in a Docker container on macOS. What's the correct way to implement this? I'd really appreciate any guidance on this.