Forum Discussion

Oxenburger_1420's avatar
Oxenburger_1420
Icon for Nimbostratus rankNimbostratus
Jan 30, 2014
Solved

iRule to change URI Path

Hi,

I'm trying to create an iRule that matches URI starting with “eat9.raspberry.cheesecake.com/cache//()” to resolves to “eat9.raspberry.cheesecake.com/(*)” via the F5

i.e. “eat9.raspberry.cheesecake.com/cache/1.1.201311042258/js/jquery.js” resolves to “eat9.raspberry.cheesecake.com/js/jquery.js”

Here is the iRule, however it keeps failing at the HTTP::redirect with error undefined procedure;

when HTTP_REQUEST { if { [HTTP::host] eq "eat1.blueberry.cheesecakes.com" } { pool PL_eat1.blueberry.cheesecakes.com_1443 } elseif { [HTTP::host] eq "eat2.blueberry.cheesecakes.com" } { pool VSEMPINTWEB07_1444 } elseif { [HTTP::host] eq "eat3.blueberry.cheesecakes.com" } { pool PL_eat3.blueberry.cheesecakes.com_1444 } elseif { [HTTP::host] eq "eat4.blueberry.cheesecakes.com" } { pool PL_eat4.blueberry.cheesecakes.com_1444 } elseif { [HTTP::host] eq "eat5.blueberry.cheesecakes.com" } { log local0.alert "hit for eat5.blueberry.cheesecakes.com -> PL_eat5.blueberry.cheesecakes.com_1444" pool PL_eat5.blueberry.cheesecakes.com_1444 } elseif { [HTTP::host] eq "eat7.blueberry.cheesecakes.com" } { pool PL_eat7.blueberry.cheesecakes.com_1444 } elseif { [HTTP::host] eq "eat8.blueberry.cheesecakes.com" } { pool PL_eat8.blueberry.cheesecakes.com_1444 } elseif { [HTTP::host] eq "eat9.blueberry.cheesecakes.com" } { pool PL_eat9.blueberry.cheesecakes.com_1444 } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/foneat") } { pool PL_eat9.blueberry.cheesecakes.com_1444 } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/css") } { pool PL_eat9.blueberry.cheesecakes.com_1444 } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/templates") } { pool PL_eat9.blueberry.cheesecakes.com_1444 } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/js") } { pool PL_eat9.blueberry.cheesecakes.com_1444 } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/images") } { pool PL_eat9.blueberry.cheesecakes.com_1444 } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/cache/*") } {

 Get the index of the last URI directory
set depth [URI::path [HTTP::uri] depth]

 Parse the last directory in the path
set last_dir [URI::path [HTTP::uri] $depth $depth]

 Parse everything after the last directory
set last_string [string trimleft [string range string $last_dir end]]
}

{ HTTP::redirect "[HTTP::host]/$last_dir$last_string" } { pool PL_eat9.blueberry.cheesecakes.com_1444 } }

Thanks,

Dave

  • There are two problems with your rule. Look at the "first problem" comment which I have added below. This test will catch all requests with the eat9 domain. The subsequent eat9 tests will never execute.

     

    Secondly, you have a misplaced closing bracket. See my "second problem" comment below. This bracket should be moved below the susequent pool statement. The effect of this is that every execution of the rule will attempt to evaluate $last_dir and $last_string, but neither of which have been defined as the definition is in the dead code after my first comment.

     

    Thirdly, I would rewrite the whole thing with a switch statement instead of multiple elseif.

     

    when HTTP_REQUEST { 
        if { [HTTP::host] eq "eat1.blueberry.cheesecakes.com" } { 
            pool PL_eat1.blueberry.cheesecakes.com_1443 
        } elseif { [HTTP::host] eq "eat2.blueberry.cheesecakes.com" } { 
            pool VSEMPINTWEB07_1444 
        } elseif { [HTTP::host] eq "eat3.blueberry.cheesecakes.com" } { 
            pool PL_eat3.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat4.blueberry.cheesecakes.com" } { 
            pool PL_eat4.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat5.blueberry.cheesecakes.com" } { 
            log local0.alert "hit for eat5.blueberry.cheesecakes.com -> PL_eat5.blueberry.cheesecakes.com_1444" 
            pool PL_eat5.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat7.blueberry.cheesecakes.com" } { 
            pool PL_eat7.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat8.blueberry.cheesecakes.com" } { 
            pool PL_eat8.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat9.blueberry.cheesecakes.com" } {    first problem
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/foneat") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/css") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/templates") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/js") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/images") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/cache/*") } {
    
             Get the index of the last URI directory
            set depth [URI::path [HTTP::uri] depth]
    
             Parse the last directory in the path
            set last_dir [URI::path [HTTP::uri] $depth $depth]
    
             Parse everything after the last directory
            set last_string [string trimleft [string range string $last_dir end]]
        }    second problem 
    
        { HTTP::redirect "[HTTP::host]/$last_dir$last_string" } 
        { pool PL_eat9.blueberry.cheesecakes.com_1444 } 
    }

9 Replies

  • There are two problems with your rule. Look at the "first problem" comment which I have added below. This test will catch all requests with the eat9 domain. The subsequent eat9 tests will never execute.

     

    Secondly, you have a misplaced closing bracket. See my "second problem" comment below. This bracket should be moved below the susequent pool statement. The effect of this is that every execution of the rule will attempt to evaluate $last_dir and $last_string, but neither of which have been defined as the definition is in the dead code after my first comment.

     

    Thirdly, I would rewrite the whole thing with a switch statement instead of multiple elseif.

     

    when HTTP_REQUEST { 
        if { [HTTP::host] eq "eat1.blueberry.cheesecakes.com" } { 
            pool PL_eat1.blueberry.cheesecakes.com_1443 
        } elseif { [HTTP::host] eq "eat2.blueberry.cheesecakes.com" } { 
            pool VSEMPINTWEB07_1444 
        } elseif { [HTTP::host] eq "eat3.blueberry.cheesecakes.com" } { 
            pool PL_eat3.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat4.blueberry.cheesecakes.com" } { 
            pool PL_eat4.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat5.blueberry.cheesecakes.com" } { 
            log local0.alert "hit for eat5.blueberry.cheesecakes.com -> PL_eat5.blueberry.cheesecakes.com_1444" 
            pool PL_eat5.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat7.blueberry.cheesecakes.com" } { 
            pool PL_eat7.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat8.blueberry.cheesecakes.com" } { 
            pool PL_eat8.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat9.blueberry.cheesecakes.com" } {    first problem
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/foneat") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/css") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/templates") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/js") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/images") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/cache/*") } {
    
             Get the index of the last URI directory
            set depth [URI::path [HTTP::uri] depth]
    
             Parse the last directory in the path
            set last_dir [URI::path [HTTP::uri] $depth $depth]
    
             Parse everything after the last directory
            set last_string [string trimleft [string range string $last_dir end]]
        }    second problem 
    
        { HTTP::redirect "[HTTP::host]/$last_dir$last_string" } 
        { pool PL_eat9.blueberry.cheesecakes.com_1444 } 
    }
    • uni_87886's avatar
      uni_87886
      Icon for Cirrostratus rankCirrostratus
      Lastly, please use the Preformatted Code button when pasting code. It is a real pain reading otherwise, and people are less likely to bother trying to read your code.
    • Oxenburger_1420's avatar
      Oxenburger_1420
      Icon for Nimbostratus rankNimbostratus
      Hi, I've worked how to strip the first two directories of the URI and parse the remaining subdirectories + basename + query string, which are then used to change the URI Path. I've posted the info here - link text Unfortunately couldn't get switch -glob to work but will keep trying. using elsif for now. Thanks, Dave
  • uni's avatar
    uni
    Icon for Altostratus rankAltostratus

    There are two problems with your rule. Look at the "first problem" comment which I have added below. This test will catch all requests with the eat9 domain. The subsequent eat9 tests will never execute.

     

    Secondly, you have a misplaced closing bracket. See my "second problem" comment below. This bracket should be moved below the susequent pool statement. The effect of this is that every execution of the rule will attempt to evaluate $last_dir and $last_string, but neither of which have been defined as the definition is in the dead code after my first comment.

     

    Thirdly, I would rewrite the whole thing with a switch statement instead of multiple elseif.

     

    when HTTP_REQUEST { 
        if { [HTTP::host] eq "eat1.blueberry.cheesecakes.com" } { 
            pool PL_eat1.blueberry.cheesecakes.com_1443 
        } elseif { [HTTP::host] eq "eat2.blueberry.cheesecakes.com" } { 
            pool VSEMPINTWEB07_1444 
        } elseif { [HTTP::host] eq "eat3.blueberry.cheesecakes.com" } { 
            pool PL_eat3.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat4.blueberry.cheesecakes.com" } { 
            pool PL_eat4.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat5.blueberry.cheesecakes.com" } { 
            log local0.alert "hit for eat5.blueberry.cheesecakes.com -> PL_eat5.blueberry.cheesecakes.com_1444" 
            pool PL_eat5.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat7.blueberry.cheesecakes.com" } { 
            pool PL_eat7.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat8.blueberry.cheesecakes.com" } { 
            pool PL_eat8.blueberry.cheesecakes.com_1444 
        } elseif { [HTTP::host] eq "eat9.blueberry.cheesecakes.com" } {    first problem
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/foneat") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/css") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/templates") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/js") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/images") } { 
            pool PL_eat9.blueberry.cheesecakes.com_1444 
        } elseif { ([HTTP::host] eq "eat9.raspberry.cheesecakes.com") && ([HTTP::uri] starts_with "/cache/*") } {
    
             Get the index of the last URI directory
            set depth [URI::path [HTTP::uri] depth]
    
             Parse the last directory in the path
            set last_dir [URI::path [HTTP::uri] $depth $depth]
    
             Parse everything after the last directory
            set last_string [string trimleft [string range string $last_dir end]]
        }    second problem 
    
        { HTTP::redirect "[HTTP::host]/$last_dir$last_string" } 
        { pool PL_eat9.blueberry.cheesecakes.com_1444 } 
    }
    • uni's avatar
      uni
      Icon for Altostratus rankAltostratus
      Lastly, please use the Preformatted Code button when pasting code. It is a real pain reading otherwise, and people are less likely to bother trying to read your code.
    • Oxenburger_1420's avatar
      Oxenburger_1420
      Icon for Nimbostratus rankNimbostratus
      Hi, I've worked how to strip the first two directories of the URI and parse the remaining subdirectories + basename + query string, which are then used to change the URI Path. I've posted the info here - link text Unfortunately couldn't get switch -glob to work but will keep trying. using elsif for now. Thanks, Dave
  • Hi,

     

    Thanks for the reply. Not sure how (first comment) eat9 test can catch all requests as is for the subdomain blueberry. subsequent test are for subdomain raspberry.

     

    Thanks for the tip with switch I'll look into it.

     

    PS. Yeah It could be an issue with my browser although I was able edit this question the edit window wasn't saving the double tilde I placed before and after the code, so I ended up having post another question.

     

    Regards,

     

    Dave

     

    • uni's avatar
      uni
      Icon for Altostratus rankAltostratus
      Oops. OK, ignore the first bit.
  • Hi,

     

    I've worked how to strip the first two directories of the URI and parse the remaining subdirectories + basename + query string, which are then used to change the URI Path.

     

    I've posted the info here -

     

    link text

     

    Unfortunately couldn't get switch -glob to work but will keep trying. using elsif for now.

     

    Thanks,

     

    Dave