Forum Discussion

SamiK_85475's avatar
SamiK_85475
Icon for Nimbostratus rankNimbostratus
Jan 09, 2011

Changing the host name, part of the URI and then redirect

Hello,

 

 

I've seen lots of ideas close to what I'm trying to accomplish, but I still seem to run into problems. I hope you can help.

 

 

I'm trying catch all http requests, if the host name contains "acb.com" and if the URI starts with "/se", and redirect them to www.xyz.com/sv/orginal_uri_without_/se . Thus /se/"rest of the uri" needs to be replaced with /sv/"rest of the uri".

 

 

E.g. any.abc.com/se/pages/index.htm -----> www.xyz.com/sv/pages/index.htm

 

 

 

Here's what I have:

 

 

when HTTP_REQUEST {

 

if { [HTTP::host]} contains "acb.com"

 

if { [HTTP::uri] starts_with "/se" } {

 

HTTP::uri "/sv[string range $uri 3 end]"

 

HTTP::redirect ]

 

}

 

}

 

 

I'm fairly new with iRules so any help to make this work would be great!

 

 

Thanks,

 

Sami
  • I'm fairly new to string map so my apologies if this isn't the right solution.

    when HTTP_REQUEST {
      if { [string tolower [HTTP::host]] contains "acb.com" } {
         if { [string tolower [HTTP::uri]] starts_with "/se" } {
              HTTP::redirect "http://www.xyz.com[string map {/se /sv} [HTTP::uri]]" 
         }
       }
    }
    
    This will replace any instance of /se with /sv if both the host and uri conditions are met so it should almost certainly be more specific using "range" as you did. I'm going to quick read up on range and will hopefully update it accordingly.
  • string range might be slightly more efficient and is more exact. The reason that updating the URI with HTTP::uri $newuri and then accessing it with [HTTP::uri] didn't work is that the values for most HTTP:: commands are cached within the same event and event priority.

     

     

    Aaron
  • Thanks Chris! Will try your code tomorrow. That string map replacement might be enough. However, if you figure out how to make this work with range - awesome.

     

     

    Hoolio, I think you explained why I saw strange replies after modifying the code. I though it must be the cache some how.

     

     

    Cheers,

     

    Sami
  • You pretty much had it in your first post:

    
    when HTTP_REQUEST {
    
       if { [string tolower [HTTP::host]] ends_with "acb.com" and [HTTP::uri] starts_with "/se" }{
          HTTP::redirect http://www.xyz.com/sv[string range [HTTP::uri] 3 end]
       }
    }
    

    Aaron
  • Something like this Aaron?

    
    when HTTP_REQUEST {
      if { [string tolower [HTTP::host]] contains "acb.com" } {
         if { [string tolower [HTTP::uri]] starts_with "/se" } {
              HTTP::redirect "http://www.xyz.com/sv[string range [HTTP::uri] 3 end]" 
         }
       }
    }
    
  • BTW, if I wanted to HTTP::respond (with 301) instead of HTTP::redirect (is 302), would that be entirely different beast?
  • Worked beautifully, thanks!

     

    I'm having another problem now with multible irules, which cause "TCL redirect/respond not allowed" but I'll try first firgure it out by myself.