Forum Discussion

PeterC_70986's avatar
PeterC_70986
Icon for Nimbostratus rankNimbostratus
Apr 18, 2013

Help with a redirect while changing a small part of the URI

I am trying to do a redirect which will look like below:

 

hostname.domain.com/abc/varying_remainder_uri redirect to NEWhostname.domain.com/def/varying_remainder_uri

 

I did the below which I know is not correct:

 

when HTTP_REQUEST {

 

if { [HTTP::uri] contains "abc"} {

 

HTTP::redirect "]"

 

}

 

}

 

 

 

It does the redirect, but keeps the "abc" in the uri so the resulting redirect looks like this:

 

https://NEWhostname.domain.com/def/...der_of_URI

 

4 Replies

  • Assuming the /abc/ and /def/ portions of the URI are static, you could use some string mapping:

    
    when HTTP_REQUEST {
         if { [string tolower [HTTP::uri]] starts_with "/abc"} {
              set newuri [string map {"/abc/" "/def/"} [string tolower [HTTP::uri]]]
              HTTP::redirect "https://NEWhostname.domain.com$newuri"
         }
    }
    
  • Thanks Kevin, Yes the abc and def parts of the uri are static. I tried the rule you listed, but it does not seem to be working.

     

  • Let's try some logging then to verify if the iRule is producing what you want:

    
    when HTTP_REQUEST {
         log local0. "original URL = [HTTP::host][HTTP::uri]"
         if { [HTTP::uri] starts_with "/abc/"} {
              set newuri [string map {"/abc/" "/def/"} [string tolower [HTTP::uri]]]
              log local0. "new URL = https://NEWhostname.domain.com$newuri"
              HTTP::redirect "https://NEWhostname.domain.com$newuri"
         }
    }
    

    Please report back what the logs return.
  • Scratch that Kevin, I went back and tried it again before putting the logging on and it worked fine. Maybe something was "stuck" in my browser session that cleared. Thanks so much for the help!!