skip to Main Content

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

3

Answers


  1. Chosen as BEST ANSWER

    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>
    

  2. Rewrite Condition if the query string contains a word "demo":

    <VirtualHost *:8080>          
        <Directory /var/www/example/>
            Allow From All
            RewriteEngine On
            RewriteCond %{SERVER_PORT} !^8085$
            RewriteCond %{QUERY_STRING} (demo)
            RewriteRule ^Demo http://%{HTTP_HOST}:%{SERVER_PORT}/$1 [L,R]
        </Directory>
    </VirtualHost>
    

    Rewrite condition if the query string does not contain the word "demo":

    <VirtualHost *:8085>          
        <Directory /var/www/example/>
            Allow From All
            RewriteEngine On
            RewriteCond %{SERVER_PORT} !^8080$
            RewriteCond %{QUERY_STRING} (!demo)
            RewriteRule ^Demo http://%{HTTP_HOST}:%{SERVER_PORT}/$1 [L,R]
        </Directory>
    </VirtualHost>
    

    if the SERVER_PORT doesn’t work, please try the port directly like below:

    RewriteRule ^Demo http://%{HTTP_HOST}:8085/$1 [L,R]
    
    Login or Signup to reply.
  3. Try:

    <VirtualHost *:8080>
    #some conf
    RewriteCond %{REQUEST_URI} demo [OR]
    RewriteCond %{QUERY_STRING} demo
    RewriteRule ^ http://%{HTTP_HOST}:8085%{REQUEST_URI} [QSA,L]
    #some more conf
    </VirtualHost>
    

    OR

    <VirtualHost *:8085>
    #some conf
    RewriteCond %{REQUEST_URI} !demo
    RewriteCond %{QUERY_STRING} !demo
    RewriteRule ^ http://%{HTTP_HOST}:8080%{REQUEST_URI} [QSA,L]
    #some more conf
    </VirtualHost>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search