Forum Discussion

NAWAZ_175679's avatar
NAWAZ_175679
Icon for Nimbostratus rankNimbostratus
Jun 13, 2018

Modificatioin of irule to append www

I have my irule like this

when HTTP_REQUEST { if { [HTTP::path] equals "/" } { HTTP::redirect "/XYZ.Web/Login.aspx" }

}


When user type https://www.abc.com      and it works fine
but when user types abc.com it gives error so i wanted to add something like this 

when HTTP_REQUEST {
if { ! [HTTP::host] starts_with "www" } {
    HTTP::redirect http://www.[getfield [HTTP::host] ":" 1][HTTP::uri]
}

}

Should i do another irule or i can add if statement under same HTTP_REQUEST ?

2 Replies

  • Hi Nawaz,

    You right, you can use HTTP_REQUEST event to process you redirection. This event is used when the system fully parses the complete client HTTP request headers (that is, the method, URI, version, and all headers, not including the HTTP request body)...

    In your case you can do it more easly:

    when HTTP_REQUEST {
    
    set hostname [string tolower [HTTP::host]]
    set uri [HTTP::uri]
    
    if { ! ($hostname starts_with "www.abc.com") } {
        HTTP::redirect "http://www.abc.com$uri"
        return
    }
    
    if { [HTTP::path] equals "/" } {
    
        HTTP::redirect "/XYZ.Web/Login.aspx"
        return
    }
    }
    

    Regards

  • Please explain what you want to do...

     

    in description, you talk about https:// and in irule, you redirect to http service

     

    So :

     

    You can try this code:

     

    when HTTP_REQUEST {
        set host [HTTP::host]
    
         evaluate host starting with "www." if we need to insert it in redirect.
        set redirectHost [expr {$host starts_with "www" ? [getfield $host ":" 1] : "www.[getfield $host ":" 1]"} ]
    
         evaluate if service is http or https / host starts with "www." If one of these condition is false, prepend to the redirect URI https://$redirectHost
        set redirectURI [expr {![PROFILE::exists clientssl] || !($host starts_with "www.") ? "https://$redirectHost" : ""}]
    
         If path equals / change URI to "/XYZ.Web/Login.aspx"
        append redirectURI [expr {[HTTP::path] equals "/" ? "/XYZ.Web/Login.aspx" : [HTTP::uri]}]
    
         if the URI requires to redirect,redirect to $redirectURI, else, forward to pool as configured in virtual server.
        if {!($redirectURI equals [HTTP::uri])} {
            HTTP::redirect $redirectURI
        }
    }