• Home
  • DevOps
  • How To
  • Cloud
  • Security
  • Monitoring
  • Web
  • OS
tutorials-space.com
Menu
  • Home
  • DevOps
  • How To
  • Cloud
  • Security
  • Monitoring
  • Web
  • OS
Home  /  Apache • Security • ssl  /  How to Redirect HTTP to HTTPS on Apache2
How to Redirect HTTP to HTTPS on Apache2
30 December 2018

How to Redirect HTTP to HTTPS on Apache2

Written by aghouchaf
Apache, Security, ssl Leave a Comment

Table of Contents

  • 1. Introduction
  • 2. Redirect HTTP to HTTPS on Apache Using .htaccess File
  • 3. Redirect HTTP to HTTPS on Apache Virtual Host

1. Introduction

HTTP (Hyper Text Transfer Protocol) is a popular as well as the fundamental protocol for data communication on the World Wide Web (WWW); typically between a web browser and the server which stores web files. Whereas HTTPS is the secure version of HTTP, where the ‘S‘ at the end stands for ‘Secure‘.

2. Redirect HTTP to HTTPS on Apache Using .htaccess File

For this method, make sure mod_rewrite is enabled:

For Ubuntu or Debian :

$ sudo a2enmod rewrite	

For Centos or RHEL :

For CentOS/RHEL users, ensure that your have the following line in httpd.conf (mod_rewrite support – enabled by default).

LoadModule rewrite_module modules/mod_rewrite.so

Now you just need to edit or create .htaccess file in your domain root directory and add these lines to redirect http to https.

RewriteEngine On  
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]

3. Redirect HTTP to HTTPS on Apache Virtual Host

Edit website VirtualHost in Apache configuration file and add the following options. Change www.example.com with your actual domain name.

With this second option you can configure your virtual host file. Normally, there are two important sections of a virtual host configurations if an SSL certificate is enabled; the first contains configurations for the non-secure port 80.

The second is for the secure port 443. To redirect HTTP to HTTPS for all the pages of your website, first open the appropriate virtual host file. Then modify it by adding the configuration below.

NameVirtualHost *:80
<VirtualHost *:80>
   ServerName www.yourdomain.com
   Redirect / https://www.yourdomain.com
</VirtualHost>

<VirtualHost _default_:443>
   ServerName www.yourdomain.com
   DocumentRoot /usr/local/apache2/htdocs
   SSLEngine On
# etc...
</VirtualHost>

Save and close the file, then restart the HTTP sever like this.

for Ubuntu/Debian :

sudo systemctl restart apache2

for RHEL/CentOS :

sudo systemctl restart httpd

aghouchaf

 Previous Article How It Works Let’s Encrypt ?
Next Article   Setup Nginx HTTP Server Self-Signed SSL/TLS Certificates on Ubuntu16.04 or 18.04

Related Posts

  • Create a Self-Signed SSL Certificate using Apache2 in Ubuntu 18.04

    January 13, 2019
  • Setup Nginx HTTP Server Self-Signed SSL/TLS Certificates on Ubuntu16.04 or 18.04

    December 30, 2018
  • How It Works Let’s Encrypt ?

    December 30, 2018

Leave a Reply

Cancel reply

Recent Posts

  • Install ELK On Ubuntu 18.04
  • Create a Self-Signed SSL Certificate using Apache2 in Ubuntu 18.04
  • How to install Google Chrome browser on Ubuntu 18.04
  • (no title)
  • Setup Nginx HTTP Server Self-Signed SSL/TLS Certificates on Ubuntu16.04 or 18.04

Recent Comments

    Archives

    • March 2019
    • January 2019
    • December 2018
    • March 2014

    Categories

    • Apache
    • Browser
    • centos
    • couchbase
    • Databases
    • debian
    • DevOps
    • ELK
    • How To
    • linux
    • network
    • nginx
    • Security
    • ssh
    • ssl
    • tls
    • Web

    Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org
    Advertisement
    • Popular Posts
    • Recent Posts
    • Install ELK On Ubuntu 18.04 March 24, 2019
    • Blog Post with Right Sidebar March 16, 2014
    • What DevOps Means? December 26, 2018
    • DevOps practices that you must implement in 2019 December 26, 2018
    • Install ELK On Ubuntu 18.04 March 24, 2019
    • Create a Self-Signed SSL Certificate using Apache2 in Ubuntu 18.04 January 13, 2019
    • How to install Google Chrome browser on Ubuntu 18.04 January 4, 2019
    • December 31, 2018

    Categories

    • Apache (6)
    • Browser (1)
    • centos (1)
    • couchbase (1)
    • Databases (1)
    • debian (2)
    • DevOps (3)
    • ELK (1)
    • How To (8)
    • linux (5)
    • network (1)
    • nginx (2)
    • Security (9)
    • ssh (1)
    • ssl (3)
    • tls (2)
    • Web (6)
    May 2022
    M T W T F S S
    « Mar    
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  

    Tags

    apache web

    Social Media

    • Connect on Facebook
    • Connect on Twitter
    • Connect on Google+
    • Connect on Pinterest

    Tags

    apache web

    Archives

    • March 2019 (1)
    • January 2019 (2)
    • December 2018 (15)
    • March 2014 (2)

    Random Posts

    • Blog Post with Right Sidebar March 16, 2014
    • How to Secure Network Services with TCP Wrappers on Linux December 28, 2018

    © Copyright 2014. http://tutorials-space.com.