Home / Redirect HTTP traffic to HTTPS in Apache HTTPD

Redirect HTTP traffic to HTTPS in Apache HTTPD

By default, all HTTP requests are sent to port 80 and HTTPS to port 443.  The rewrite rule was added to the httpd.conf file forces all requests to be redirected to HTTPS (port 443).

  • You can redirect all website traffic from HTTP (port 80 or 8080) to HTTPS (port 443 or 8443) by adding the following code at the bottom of the httpd.conf file.

# Enable rewrite
RewriteEngine on

# All request to root will be redirected to HTTPS
RewriteRule ^/$ https://%{HTTP_HOST}/ [R,L]

# All request to any context will be redirected to HTTPS (with the content)
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R,L]

  • The second method is to use mod_rewrite in your httpd.conf to redirect HTTP to HTTPS in httpd.conf file.

LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
<VirtualHost *:80>
<IfModule mod_rewrite.c>
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [L,R] </IfModule>
</VirtualHost>

  • The third method is as follows.

LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{SERVER_PORT} =80
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R]

  • The fourth method is as follows.

LoadModule rewrite_module modules/mod_rewrite.so
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [L,R]

Leave a Reply