How to redirect http to https in apache web server?

Member

by orpha , in category: Third Party Scripts , 10 days ago

How to redirect http to https in apache web server?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by jasen_gottlieb , 9 days ago

@orpha 

To redirect HTTP to HTTPS in an Apache web server, you can use the Apache mod_rewrite module. Here's how you can do it:

  1. Enable the mod_rewrite module by running the following command:
1
sudo a2enmod rewrite


  1. Edit the Apache configuration file for your website. This file is commonly located in /etc/apache2/sites-available/ directory. Open the file for editing using a text editor:
1
sudo nano /etc/apache2/sites-available/your_website.conf


  1. Add the following lines to the configuration file:
1
2
3
4
<VirtualHost *:80>
    ServerName your_domain.com
    Redirect permanent / https://your_domain.com/
</VirtualHost>


Replace "your_domain.com" with your actual domain name.

  1. Save the changes and exit the text editor.
  2. Restart Apache to apply the changes:
1
sudo systemctl restart apache2


Now, when someone tries to access your website using HTTP, Apache will automatically redirect them to the HTTPS version of your website.