Tuesday, April 19, 2022

[SOLVED] Apache HTTP server redirect to port 8085 IF url contains "demo" ELSE redirect to port 8080

Issue

I have 2 applications running on port 8080 and 8085. Both has different URL queries/paths and application on port 8085 have keyword demo on all of its URL queries/paths and other doesn't. So it acts as an unique identifier

Can I make any change to /etc/httpd/conf/httpd.conf file so that if the server identifies demo as the keyword then request will reach application on port 8085, if not then request will reach port application on port 8080 ?

If there is a way, please provide sample configuration which goes into httpd.conf file

EDIT 1:

After trying from first 2 of below answers, I couldn't achieve this. I don't know if ProxyPass and ProxyPassReverse are not allowing to achieve this. I tried commenting them, adding them in VirtualHost etc. But did not help.

This is the flow we are expecting: User will hit URL (without mentioning port) like - https://example.com/demo and this will be routed to app on port 8085 else routed to 8080

May be taking look at my complete httpd.conf might help

Link to my httpd.conf - https://gofile.io/d/tWIHvX


Solution

Note: self answer after being able to achieve this

Achieved this using LocationMatch directive easily. Below is the configuration.

As this is regex dependent you can change to check conditions like ends with, starts with, contains and etc. And both the below LocationMatch conditions are in my httpd.conf

1.If URL like 192.168.1.113/demo then go to 8085

<LocationMatch "^/(demo)(.*)$">
  RewriteEngine on
  ProxyPass         http://localhost:8085/ nocanon
  ProxyPassReverse  http://localhost:8085/
</LocationMatch>

2.If URL not like 192.168.1.113/demo then go to 8080

<LocationMatch "^/((?!demo).)*$">
  RewriteEngine on
  ProxyPass         http://localhost:8080/ nocanon
  ProxyPassReverse  http://localhost:8080/
</LocationMatch>


Answered By - Chandan
Answer Checked By - Senaida (WPSolving Volunteer)