Forum Discussion

G__246_ran___19's avatar
G__246_ran___19
Icon for Altostratus rankAltostratus
Mar 08, 2017

Redirect when both Host and Path are equal

Hello, I have a virtual server who has many different host names and path, one of those i need to have a redirect like this: If hostname is and path is / redirect to

How shod I write the iRule? can any help me, I cannot do a redirect from / to 123 cause it will affect the other sites.

2 Replies

  • Hi Göran,

    you may try either a nested

    [switch]
    or
    [if]
    condition to parse the HOST and then the Path (general purpose) or use a combined [if] condition (tailordered purpose).

    Example 1: Using general purpose nested

    [switch]
    commands.

    when HTTP_REQUEST {
        switch -glob -- [string tolower [HTTP::host]] {
            "www.domain.de" {
                 Requests for www.domain.com
                switch -glob -- [string tolower [HTTP::path]] {
                    "/" {
                         Requests for URI = /
                        HTTP::redirect "/abc/" 
                    }
                    default {
                         Requests for URI = *
                    }
                }
            }
            "www.domain.com" {
                 Requests for www.domain.com
            }
            "www.domain.net" {
                 Requests for www.domain.net
            }
            default {
                 Unknown HOST-name
            }
        }   
    }
    

    Example 2: Using general purpose nested

    [if]
    commands.

    when HTTP_REQUEST {
        set low_host [string tolower [HTTP::host]]
        if { $low_host eq "www.domain.de" } then {
             Requests for www.domain.de
            set low_path [string tolower [HTTP::path]]
            if { $low_path equals "/" } then {
                 Requests for URI = /
                HTTP::redirect "/abc/" 
            } else {
                 Requests for URI = *
            }   
        } elseif { $low_host equals "www.domain.com" } then {
             Requests for www.domain.com
            }
        } elseif { $low_host equals "www.domain.net" } then {
             Requests for www.domain.net
        } else {
             Unknown HOST-name
        }   
    }
    

    Example 3: Using a tailordered

    [if]
    command (to support just your requirements)

    when HTTP_REQUEST {
        if { ( [string tolower [HTTP::host]] equals "www.domain.de" ) 
         and ( [HTTP::path] equals "/" } then {
             Requests for www.domain.de/
            HTTP::redirect "/abc/" 
        } else {
              Request for another HOST-name or another Path
        }   
    }
    

    Note: The general purpose examples can be easily extended to support various other things. The benefit of such a syntax is that you get scalable and unified method to control your traffic as needed.

    Cheers, Kai

  • In general, this type of redirecting based on rules should be done using LTM policies for which you can add rules that match on multiple objects and are much more scalable than dealing with writing and rewriting iRules. They are also more performant as they are built-in and use a selection tree rather than runtime decisions. These were available in v11.4+