How to redirect http to https on Apache2

rimsovankiry
1 min readMar 7, 2021

--

Redirect HTTP To HTTPS

This is my favorite snippet code in Apche2 for redirect http protocol to https protocol. There are only two steps for completed this.

Step 1: Enable a2enmod rewrite

Assume, you are in terminal of server and logged as root permission

$ a2enmod rewrite

Step 2: Update sites-available conf file

Assume that my site conf file is default-ssl.conf, you can change to your actual conf file of sites-available.

$ nano /etc/apache2/sites-available/default-ssl.conf

Copy this code to the last of files

     RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

Than save files and restart service apache2.

$ sudo service apache2 restart

Conclusion

In this article, you’ve setup redirect http to https by using a2enmode rewrite.

Don’t forget click Clap to help hundreds of other creative people whom it might be useful for as well. So Clap! Clap! :)👏🏼

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

rimsovankiry
rimsovankiry

Written by rimsovankiry

Passionate Web3, AI and technology 🚀

No responses yet

Write a response