Forum Discussion

donmunyak_10415's avatar
donmunyak_10415
Icon for Nimbostratus rankNimbostratus
Jul 24, 2009

Rewrite & Redirect user login

v9.4.3 iRule

 

 

Testing for

 

 

 

 

to be rewritten and/OR redirected to:

 

 

 

 

The initial URL is allowed because DNS has a wildcard 'A' record entry.

 

 

The Application team wants us to take the URL from the initial client GET request, and

 

append to rewritten URL, as the user id.

 

 

I know its poor planning, but the application team sees no reason to expect

 

an https initial GET request.

 

 

 

 

save initial GET host header

 

set data [HTTP::host]

 

 

when HTTP_REQUEST {

 

 

Check for http://fname.lname.mycompany.com and redirect with id="fname.lname.mycompany.com"

 

 

if {[HTTP::host] string match {/[a-zA-Z0-9]*\.?[a-zA-Z0-9]*\."mycompany.com"}} {

 

HTTP::redirect "https://application.store.com/app/jsp/initialGMCLanding.jsp?id=[HTTP::host]"

 

}

 

 

Check for wildcard http://*.mycapitalone.com and redirect with "NO" id.

 

 

if {[HTTP::host] string match {/*\."mycompany.com"}} {

 

HTTP::redirect "https://application.store.com/app/jsp/initialGMCLanding.jsp"

 

}

 

 

}

 

}

3 Replies

  • Hi,

    The format for string match should be:

    % string match

    wrong args: should be "string match ?-nocase? pattern string"

    And you shouldn't escape periods in a string comparison. They do not have a special meaning of "any character" like they do in regex.

    The downside to a string match is that you can't use a wildcard for number of times a character class is repeated. A * will match any character zero to any number of times.

    So you can check if the hostname matches x.y.example.com using:

    string match -nocase {[a-z0-9]*.[a-z0-9]*.example.com} [HTTP::host]

    But scan (Click here) will give you the ability to match a character class any number of times and the ability to save matches. Here are a few examples:

    scan returns a count of the number of matches assuming you provide variables to save the matches in

    % scan test1.test2.example.com {%[a-z0-9].%[a-z0-9].%[a-z0-9].%[a-z0-9]} a b domain tld

    4

    % puts "$a $b $domain $tld"

    test1 test2 example com

    Or:

    % scan test1.test2.example.com {%[a-z0-9].%[a-z0-9].example.com} a b

    2

    % puts "$a $b"

    test1 test2

    So, you could try something like this:

     
     when HTTP_REQUEST { 
      
         Check if host header matches pattern, a.b.example.com 
        if {[scan [string tolower [HTTP::host]] {%[a-z0-9].%[a-z0-9].example.com} a b] == 2}{ 
      
           log local0. "[IP::client_addr]:[TCP::client_port]: Matched host [HTTP::host] with $a $b" 
           HTTP::redirect "https://application.store.com/app/jsp/initialGMCLanding.jsp?id=[HTTP::host]" 
      
        } elseif {[string match -nocase {[a-z0-9]*.example.com} [HTTP::host]]}{ 
      
            Redirect to default location 
           HTTP::redirect "https://application.store.com/app/jsp/initialGMCLanding.jsp" 
      
        } else { 
            Take some default action? 
        } 
     } 
     

    Aaron
  • Hil,

     

     

    I want to redirect http to https and https to https.

     

     

    for exam http://prasanna.f5.com to https://prasanna.dev.f5.com , https://prasanna.f5.com to https://prasanna.dev.f5.com

     

     

    i write the irule like below.

     

     

    when HTTP_REQUEST {

     

    if { ([HTTP::host] equals "prasanna.f5.com") } {

     

    HTTP::respond 301 Location "https://prasanna.dev.f5.com[HTTP::uri]"

     

    }

     

    else {

     

    HTTP::respond 301 Location https://[getfield [HTTP::host] ":" 1][HTTP::uri]

     

    }

     

    }

     

     

    but the customer want now

     

     

    http://*/* -> https://prasanna.dev.f5.com/*

     

    https://*/* -> https://prasanna.dev.f5.com/*

     

     

    can you please help me on this irule.

     

     

    Regards

     

    Prasanna AR