How to redirect from one port to another?

Posted on

How to redirect from one port to another? – Managing your servers can streamline the performance of your team by allowing them to complete complex tasks faster. Plus, it can enable them to detect problems early on before they get out of hand and compromise your business. As a result, the risk of experiencing operational setbacks is drastically lower.

But the only way to make the most of your server management is to perform it correctly. And to help you do so, this article will share nine tips on improving your server management and fix some problem about apache-2.2, .htaccess, httpd, , .

I’ve a domain on which I’m running two different web applications at different ports.

  • A Java web application on example:8888/foo
  • A PHP website on example:8080/bar

The requirement is, if an user tries to access the root of example:8888 or example:8080, then the user should be redirected to example:8888/foo.

How can I achieve this requirement?

Solution :

You can add the port in a redirect (or in any URL) by adding a colon and you can match the root of the site by using regular expressions in a RewriteRule.

RewriteRule ^/$ http://example.com:8888/foo [R]

Inside a .htaccess file there are no leading slashes so this would be more appropriate:

RewriteRule ^$ http://example.com:8888/foo [R]

I would put the JAVA application up with either mod_proxy, or mod_proxy_ajp. The later in case a.) this is a tomcat (compatible application server) and b.) you have access to the configuration of the application server.

NameVirtualHost *:8888
Listen 8888

<VirtualHost *>
   ServerName example
   ServerAdmin you@the.re

   <Location />
     <IfModule mod_proxy.c>
       ProxyPass http://example:8888/foo
       ProxyPassReverse http://example:8888/foo
     </IfModule>
   </Location>

 ...
</VirtualHost>

You have to take care that your server (Apache) doesn’t relay proxy requests. Most distributions have this savely set by default, but double check it. The Apache docu is your friend: http://httpd.apache.org/docs/2.2/mod/mod_proxy.html

Port 8080 is a different matter as this is served by your application server itself. If you want to have it handled by the Apache as well, you should deactivate Catalina (Tomcat webserver) and set it up to serve via AJP.

Leave a Reply

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