How to redirect HTTP to HTTPS

If you work with the Apache web server and can change its configuration, follow the Apache documentation:

<VirtualHost *:80>
    ServerName www.example.com
    Redirect "/" "https://www.example.com/"
</VirtualHost>
 
<VirtualHost *:443>
    ServerName www.example.com
    # ... SSL configuration goes here
</VirtualHost>

But you also asked if you can do it in a .htaccess file. In that case you can use Apache's RewriteEngine:

 

RewriteEngine On
RewriteCond %{HTTPS} off

 

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
  • 0 Users Found This Useful
Was this answer helpful?

Related Articles

Install Postfix MTA on Ubuntu 16.04 / 17.10 / 18.04

Postfix is a widely used Mail Transfer Agent (MTA) that most Linux systems use to router email...

How to change the ssh port on Linux or Unix server

I am a new Linux/Unix server admin. How do I change the SSH Port for my Linux or Unix server...

How To Install Docker on Ubuntu 18.04 & 16.04 LTS

  Docker is container-based application framework, which wraps a specific application with all...

Install Apache, MariaDB, PHP and PhpMyAdmin in Ubuntu 18.04

Step 1: Install Apache Web Server on Ubuntu 18.04 1. First start by updating your software...

How to install OpenLDAP on Ubuntu 18.04

  LDAP is the Lightweight Directory Access Protocol, which allows for the querying and...