Forum Discussion

alinayesina_102's avatar
alinayesina_102
Icon for Nimbostratus rankNimbostratus
Nov 15, 2011

Dynamic URI rewrite

 

Where xxxxx is numeric variable

 

 

https://www.abc.com/uri/oldpage.aspx?xxxxxxx

 

redirect to

 

https://www.xyz.com/uri/newpage.aspx?xxxxxxx

 

 

Thank you for help!

 

  • George_Watkins_'s avatar
    George_Watkins_
    Historic F5 Account
    Something like this should work:

    when RULE_INIT {  set static::old_host "www.abc.com"  set static::old_path "/uri/oldpage.aspx"  set static::new_host "www.xyz.com"  set static::new_path "/uri/newpage.aspx"}when HTTP_REQUEST {  if { ([HTTP::host] equals $static::old_host) && ([HTTP::path] equals $static::old_path) } {    set n [HTTP::query]     if there is a GET variable assignment, you'll need to query the value as such     set n [URI::query [HTTP::uri] "some_get_var"]    if { [string length $n] != 0 } {      set redirect_url "http://${static::new_host}${static::new_path}?${n}"
          HTTP::redirect $redirect_url      log local0. "Redirected client [IP::remote_addr] to $redirect_url"    }  }}

    Fill in the static variables in RULE_INIT and it should work for your situation. If you need to pull a value from a GET variable in the query section you might need to modify it slightly (see the commented section).

    -George
  • just another example.

    [root@ve1023:Active] config  b virtual bar list
    virtual bar {
       snat automap
       pool foo
       destination 172.28.65.152:https
       ip protocol tcp
       rules myrule
       profiles {
          clientssl {
             clientside
          }
          http {}
          tcp {}
       }
    }
    [root@ve1023:Active] config  b rule myrule list
    rule myrule {
       when HTTP_REQUEST {
            if {[string tolower [HTTP::host]] equals "www.abc.com"}{
                    if {[string tolower [HTTP::uri]] contains "oldpage.aspx"}{
                            HTTP::respond 302 noserver Location "https://www.xyz.com[string map -nocase {"oldpage.aspx" "newpage.aspx"} [HTTP::uri]]"
                    }
            }
    }
    }
    
    [root@ve1023:Active] config  curl -Ik https://www.abc.com/uri/oldpage.aspx?12345
    HTTP/1.0 302 Found
    Location: https://www.xyz.com/uri/newpage.aspx?12345
    Connection: Keep-Alive
    Content-Length: 0