redirect http to https in apache on custom port

How to Redirect HTTP to HTTPS on Custom Port in Apache

Typically, websites redirect HTTP port 80 to HTTPS port 443. But sometimes, you may need to redirect HTTP from a different port (e.g. 8080) to HTTPS. In this article, we will look at how to redirect HTTP to HTTPS on Custom Port in Apache.

How to Redirect HTTP to HTTPS on Custom Port in Apache

Here are the steps to redirect HTTP to HTTPS on custom port 8080 in Apache.

Before you proceed, please note that plain HTTP runs on port 80 only. So if you access your website as browser will send the request to port 80 on your server. If you are running your web server on a different port such as 8080 then you need to use to access your website. We will be dealing with this use case in our article.

If you want to redirect a different port to HTTPS, replace 8080 in all steps below with your choice of port number.

1. Open Virtual Host File

Open terminal and run the following command to open Apache virtual host file. Please change the filename below to your website’s virtual host file.

$ sudo vi /etc/apache2/sites-available/http.conf

Also read : How to Rsync between two servers

2. Update Virtual Hosts

You need 2 virtual hosts – one listening on port 8080 and the other listening to port 443 (HTTPS)

<virtualhost *:8080="">
    RewriteEngine On
    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

<virtualhost *:443="">
    SSLEngine on
    SSLCertificateFile /var/www/ssl/certificate.crt
    SSLCertificateKeyFile /var/www/ssl/secret.key
    SSLCertificateChainFile /var/www/ssl/intermediate.crt

Let us look at the above configuration. We have create two virtual hosts – for port 8080 and port 443. In the first virtual host, we listen to port 8080, and redirect all incoming requests to HTTPS URL, that is, handled by second virtual host. In the second virtual host, we have also added ssl details.

We use the following RewriteRule to redirect all HTTP requests to HTTPS.

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

Also read : How to Install ReactJS in Ubuntu

3. Restart Apache Server

Restart Apache Server to apply changes

$ sudo service apache2 restart

That’s it. Now if you open browser and go to you will be redirected to

Also read : How to Create JSON Response Using Django

Leave a Reply

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