Forum Discussion

Daniel_S_135288's avatar
Daniel_S_135288
Icon for Nimbostratus rankNimbostratus
Mar 18, 2015

match_regex redirect

Hello Guys

 

Im need your help

 

I try to redirect a URI with a dynamic Number

 

my iRule looks like this at the moment

 

if { [HTTP::uri] matches_regex {/c/cn[0-9]{5}} } { HTTP::respond 301 Location https://www.abc.com/c/cn????? }

 

I need to redirect the page to a dynamic articlepage with the number from the URI

 

the request looks like:

 

http://www.abc.com/aaa/bbbb/cccc/c/cn75470/ddd

 

thanks for your help

 

2 Replies

  • If you want to use regex, you can do something like this:

     Match the "/cn/" and grab the  (the "0" is so the regexp can send the full string to null instead of an actual variable since you don't need it)
    [regexp -nocase {/cn(\d+)/} $a 0 id]
     $id will be 75470 in your examples case
    if { [info exists id] && $id } {
        HTTP::respond 301 Location https://www.abc.com/c/cn$id
        return
    }
    
  • hey

    finally i did id like that

        set lowuri [string tolower [HTTP::uri]]
          if { $lowuri matches_regex {/cn[0-9]{5}} } {
            set new_uri [regexp -inline {/cn[0-9]{5}} $lowuri]
            HTTP::respond 301 Location https://www.aaa.com/c/$new_uri
            return
        }
          if { [HTTP::uri] matches_regex {/cn[0-9]{4}} } {
            set new_uri [regexp -inline {/cn[0-9]{4}} $lowuri]
            HTTP::respond 301 Location  https://www.aaa.com/l/$new_uri
            return
        }
    
          if  { $lowuri matches_regex {an[0-9]{9}} } {
            set new_uri [regexp -inline {an[0-9]{9}} $lowuri]
            HTTP::respond 301 Location http://www.aaa.com/v/$new_uri
            return
        }
    

    thanks for your help Michael

    BR