Forum Discussion

pedinopa_170325's avatar
pedinopa_170325
Icon for Nimbostratus rankNimbostratus
Oct 03, 2016

irule to edit a URI

I have been working on an irule that will edit a URI string. What I need it to do is simply add a .domain.com after the first directory of the uri path and redirect to the new URI. I have it kind of working in that it is still very case sensative, but also it is not entering my switch statements. I have turned on HSL logging and can see that for some reason it is not entering or matching my switch statements. Some of the switch statements will only redirect. Below is what I have tried

incomming uri will be abc.domain.com/dirx/string what I want is dirX.domain.com/string

when HTTP_REQUEST {

HSL::send $hsl "This is the HTTP URI [HTTP::uri]"
HSL::send $hsl "This is the HTTP Path [HTTP::path]"

switch -glob { tolower [HTTP::path]} { "/DIR 1/" { set switches [string range [HTTP::uri] 19 end]

the switches will be everythi from the origional URI after the first directory
   HTTP::redirect "http://DIR1.DOMAIN.COM/$switches"
  }
 "/DIR 2" {
   set switches [string range [HTTP::path] 15 end]
the switches will be everythi from the origional URI after the first directory
   HTTP::redirect "http://Dir2.DOMAIN.COM/$switches"
  }
 "/DIR3*" {
   set switches [string range [HTTP::path] 19 end]
the switches will be everythi from the origional URI after the first directory
   HTTP::redirect "http://Dir3.DOMAIN.COM/$switches"     
 }
 "/DIR4*" {
   HTTP::redirect "http://DIR 4.DOMAIN.COM/"
  }
 "/DIR 5*" {
   HTTP::redirect "http://DIR5.DOMAIN.COM"
  }
 "/DIR 6" {
   HTTP::redirect "http://DIR6.DOMAIN.COM"
  }

}

3 Replies

  • Hi!

    You might want to try the syntax highlighting function. Otherwise your irules will be hard to interpret.

    One thing that could be the issue with your original irule was that you set the case to lower and then match against capital letters (not sure if this was just an example gone bad though).

    I might have tried to parse the directory instead of doing switch glob. Try this one?

    when HTTP_REQUEST {
    
        HSL::send $hsl "This is the HTTP URI [HTTP::uri]"
        HSL::send $hsl "This is the HTTP Path [HTTP::path]"
    
        set directory [lindex [split [string tolower [HTTP::uri]] "/"] 1]
    
        switch $directory  { 
    
            "dir1" { 
                set switches [string map {"/$directory/" "/"} [string tolower [HTTP::uri]]]
                HTTP::redirect "http://dir1.domain.com/$switches"
            }
            "dir2" {
                set switches [string map {"/$directory/" "/"} [string tolower [HTTP::uri]]]
                HTTP::redirect "http://dir2.domain.com/$switches"
            }
            "dir3" {
                set switches [string map {"/$directory/" "/"} [string tolower [HTTP::uri]]]
                the switches will be everythi from the origional URI after the first directory
                HTTP::redirect "http://dir3.domain.com/$switches"     
            }
            "dir4" {
                HTTP::redirect "http://dir4.domain.com/"
            }
            "dir5" {
                HTTP::redirect "http://dir5.domain.com"
            }
            "dir6" {
                HTTP::redirect "http://dir6.domain.com"
            }
        }
    }
    

    /Patrik

  • so I simplified my irule and my issue is that I cant get it to drop the first directory form the URI. I have been trying many different things I found here but nothing seems to work.

    First use hsl::open to define where to send logs and save it as a vaiable HSL

    when CLIENT_ACCEPTED { set hsl [HSL::open -proto UDP -pool graylog2-syslog-pool] } when HTTP_REQUEST { HSL::send $hsl "This is the HTTP URI [HTTP::uri]" HSL::send $hsl "This is the HTTP Path [HTTP::path]"

    set directory [lindex [split [string tolower [HTTP::uri]] "/"] 1]
    

    set uri [string map {"/$directory/" "/"} [string tolower [HTTP::uri]]] HSL::send $hsl "This is the new uri $uri"

    HTTP::redirect "http://$directory.domain.com/"

    when this works I will append the new uri to the redirect

    }

  • You forgot the preformatted code again. 🙂

     

    Skip the curly braces and you should be good to go. Apparently they prevent the use of variables.

     

    when CLIENT_ACCEPTED { '
        set hsl [HSL::open -proto UDP -pool graylog2-syslog-pool] 
    }
    
    when HTTP_REQUEST { 
        HSL::send $hsl "This is the HTTP URI [HTTP::uri]"
        HSL::send $hsl "This is the HTTP Path [HTTP::path]"
    
        set directory [lindex [split [HTTP::uri] "/"] 1]
        set uri [string map -nocase "/$directory/ /" [HTTP::uri] ]
    
        HSL::send $hsl "This is the new uri $uri"
    
        HTTP::redirect "http://$directory.domain.com$uri"
    
        when this works I will append the new uri to the redirect
    }
    

    /Patrik