apache redirect by port

How to Redirect Port 80 to 8080 in Apache

Sometimes you may need to redirect requests from Port 80 to 8080 in Apache web server. In this article, we will look at different ways to redirect port 80 to 8080.


How to Redirect Port 80 to 8080 in Apache .htaccess

Here are the steps to redirect from port 80 to 8080.


1. Using VirtualHost

In this case, we will add a RedirectRule in out Virtual Host file. If you have access to Apache’s default virtual host file, open it in a text editor.

$ sudo vi /etc/apache2/sites-available/000-default.conf

Look for the VirtualHost Tag for port 80 and add the following lines.

# Redirect all requests to the local Apache server to port 8080 
RewriteRule ^.*$ http://%{HTTP_HOST}:8080%{REQUEST_URI}

So your VirtualHost will look like

<VirtualHost *:80>
...
# Redirect all requests to the local Apache server to port 8080 
RewriteRule ^.*$ http://%{HTTP_HOST}:8080%{REQUEST_URI}
...
</VirtualHost>

Save and close the file.

Restart Apache server to apply changes.

$ sudo service apache2 restart


2. Using Proxy_pass

Alternatively, you may also use proxy_pass directive to redirect port 80 to 8080. In this case, Apache server will act as a reverse proxy server that receives requests to port 80 and then redirects them to port 8080.

Install & mod_proxy on your system.

sudo a2enmod proxy 
sudo a2enmod proxy_http 
sudo a2enmod proxy_balancer 
sudo a2enmod lbmethod_byrequests 
sudo systemctl restart apache2

Open virtual host file in a text editor.

$ sudo vi /etc/apache2/sites-available/000-default.conf

Add the following lines in your VirtualHost tag. Replace /url-of-your-choice with a URL of your choice.

ProxyPass /url-of-your_choice http://localhost:8080/servlet-name 
ProxyPassReverse /url-of-your-choice http://localhost:8080/servlet-name

So your Virtual Host tag will look like,

<VirtualHost *:80>
...
ProxyPass /url-of-your_choice http://localhost:8080/servlet-name 
ProxyPassReverse /url-of-your-choice http://localhost:8080/servlet-name
...
</VirtualHost>

Save and close the file.

Restart Apache server to apply changes.

$ sudo service apache2 restart


3. Using .htaccess

If you have access to .htacces file, open it in a text editor.

$ sudo vi /var/www/html/.htaccess

Add the following lines to it. Replace url_string with your URL string.

RewriteEngine On 
RewriteCond %{SERVER_PORT} =80 
RewriteRule ^url_string http://localhost:8080%{REQUEST_URI} [NC,L,R]

Restart Apache server to apply changes.

sudo service apache2 restart

In this article, we have looked at different ways to redirect port 80 to port 8080. One uses redirect directive and other uses Proxy_pass directive. You can use these steps to redirect Apache server by port number.

Also read :

How to Monitor Apache Server Performance with mod_status
How to Parse JSON in NodeJS
How to Hide PHP Version in WordPress/Apache
How to Setup SSL/HTTPS in NodeJS
How to Prevent Direct Access to PHP File

Leave a Reply

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