Open In App

How to Redirect http to https in Apache Server?

Last Updated : 09 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Redirecting HTTP traffic to HTTPS ensures that all communications between your website and its visitors are securely encrypted. This is crucial for protecting sensitive information and maintaining the integrity of data. By configuring your Apache server to redirect HTTP to HTTPS, you enhance the security and trustworthiness of your users.

Approach: Using the mod_rewrite Module

In this approach, we are going to enable the module that will allow us to edit the conf file and we can make changes according to ourself.

Steps to redirect http to https in apache server

Step 1: Enable Required Modules

Ensure mod_rewrite and mod_ssl are enabled.

sudo a2enmod rewrite
sudo a2enmod ssl

Step 2: Edit the Apache Configuration File

Open the configuration file for your site.

sudo nano /etc/apache2/sites-available/your_site.conf

Step 3: Update the HTTP VirtualHost

Add a redirect rule to the <VirtualHost *:80> block.

<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

<VirtualHost *:443>
SSLEngine on
SSLCertificateFile <path to your certificate file>
SSLCertificateKeyFile <path to your private key file>

# Rest of your site configuration
# ...
</VirtualHost>

Step 4: Restart Apache

sudo service apache2 restart

Next Article
Article Tags :

Similar Reads