How to redirect http to https and proxy pass using apache in ubuntu

This tutorial shows how to redirect your website from http to https using Apache. It also shows how to ProxyPass a web application in Apache.


For example, if your application is running in localhost:8080 you can make it redirect to localhost using Proxypass in apache.
The first step is enabling the rewrite, proxy_http and SSL mode in apache as below:

# enable more re-write and ssl in apache
sudo a2enmod rewrite
sudo a2ensite default-ssl
sudo a2enmod ssl
sudo a2enmod proxy_http
# restart apache
systemctl restart apache2

After enabling rewrite mode we need to add RewriteRule to 000-default.conf file.
In the example below, we redirect the to

# apply changes inside apache settings
# for redirecting http to https for</span>
vim /etc/apache2/sites-enabled/000-default.conf
# add lines below before </VirtualHost>
RewriteEngine  On
# LetsEncrypt
RewriteCond %{REQUEST_URI} !^/.well-known/acme-challenge/.*$
# Redirect all requests to https version of the site
RewriteRule     ^/(.*)$$1     [R]

After changing the 000-default.conf everything is redirecting from http to https. In the example below our web application is running on port 8080 and we want it to redirect to port 80 (providing the fact that port 80 is free).


In order to do that we change the default-ssl.conf file. In this file, we set the ProxyPreserveHost to on and add the ProxyPass and ProxyPassReserve

vim /etc/apache2/sites-enabled/default-ssl.conf
# add lines below before </VirtualHost> please note that the assumption is a program runs on localhost:8080
ProxyPreserveHost On
ProxyPass / http://localhost:8080/
ProxyPassReverse / http://localhost:8080/
# after saving the file restart the apache
sudo service apache2 restart

Leave a Reply

Your email address will not be published. Required fields are marked *