Forum Discussion

Steve_Romanowsk's avatar
Steve_Romanowsk
Icon for Nimbostratus rankNimbostratus
Mar 23, 2015

Redirect based on part of path

I need to have a single vhost name (this.example.com) going to multiple applications depending on the first part of the path extension, then need to eliminate that first part of the path extension while caring on the rest of the URI.

 

Example: http://this.example.com/documents/dcm/viewer.asp Goes to

 

http://this.example.com/logs/error/errormanager.asp Goes to

 

http://this.example.com/reports/report/viewer.asp Goes to

 

I have this so far but it is not working. There may be an easier way than irule that I am not familiar with.

 

when HTTP_REQUEST { if { [HTTP::host] equals "this.example.com" } { if { [string tolower [HTTP::path]] starts_with "/documents/" } { HTTP::redirect "serverA:8080" [string trimleft [HTTP::uri] /documents] } elseif { [string tolower [HTTP::path]] starts_with "/logs/" } { HTTP::redirect "serverB" [string trimleft [HTTP::uri] /logs] } elseif { [string tolower [HTTP::uri]] starts_with "/reports/" } { HTTP::redirect "serverC" [string trimleft [HTTP::uri] /reports] } else { pool http://this.example.com [HTTP::uri] } } }

 

1 Reply

  • Hi Steve,

    the
    findstr
    function will help:
    when HTTP_REQUEST { 
        if { [string tolower [HTTP::host]] equals "this.example.com" } {
            if { [string tolower [HTTP::path]] starts_with "/documents/" } {
                HTTP::redirect "http://serverA:8080[findstr [string tolower [HTTP::uri]] "/documents" [string length "/documents"]]"
            } elseif { [string tolower [HTTP::path]] starts_with "/logs/" } {
                HTTP::redirect "http://serverB[findstr [string tolower [HTTP::uri]] "/logs" [string length "/logs"]]"
            } elseif { [string tolower [HTTP::uri]] starts_with "/reports/" } {
                HTTP::redirect "http://serverB[findstr [string tolower [HTTP::uri]] "/reports" [string length "/reports"]]"
            } else {
                HTTP::redirect "http://this.example.com[HTTP::uri]"
            }
        }
    }
    

    There are smarter solutions based on the

    switch
    command (jump table).

    Please note, that there is no action defined, if the initial condition (host name) does not match. Thanks, Stephan