Forum Discussion

mikey_webb's avatar
Mar 31, 2021

irule to redirect a uri

i have a redirect based on country which works www.abc.com if in uk redirects to uk.abc.com

I need to redirect but also to retain a catalogue number that is unique for different products so for example www.abc.com/catalogue/book/3333 redirects to uk.abc.com/catalogue/book/3333 and www.abc.com/catalogue/book/4444 redirects to uk.abc.com/catalogue/book/4444

 

For this the below works well

   elseif { [HTTP::host] ends_with "abc.com" and [HTTP::uri] starts_with "/book/catalogue" } {

      HTTP::redirect "https://uk.abc.com[HTTP::uri]" 

 

however i now need to redirect a slightly different URI book/catalogue. so for example www.abc.com/book/catalogue/3333 redirects to uk.abc.com/catalogue/book/3333 and www.abc.com/book/catalogue/4444 redirects to uk.abc.com/catalogue/book/4444.

 

So this means both the following www.abc.com/book/catalogue/3333 and www.abc.com/catalogue/book/3333 need to redirect to uk.abc.com/catalogue/book/3333 - and of course similar for all otherproduct numbers.

 

can anyone help, give pointers how to do

1 Reply

  • Try following if you want to have clienside redirect

    if { [HTTP::uri] starts_with "/book/catalogue" } {
          set newuri "/catalogue/book[string range [HTTP::uri] 15 end]"
          HTTP::respond 301 Location "https://uk.abc.com$newuri"
          return
          }

    If you want to have a rewrite on the serverside and shouldn't be visible to the client browsers

    if { [HTTP::uri] starts_with "/book/catalogue" } {
          set newuri "/catalogue/book[string range [HTTP::uri] 15 end]"
          HTTP::uri $newuri
          return
          }