Forum Discussion

CAMES_13251's avatar
CAMES_13251
Icon for Nimbostratus rankNimbostratus
Jun 20, 2011

Remove URI if it doesn't match my options?

Hi All,

 

 

I'm quite new to irules, and can't get something working that should be quite simple.

 

 

What I am trying to do is redirect the request based on a couple of key words in the URI, but if the URI does not contain one of these, remove the URI completely and just go to the main site.

 

 

So, if the host is www.test.com...

 

 

http://www.test.com/ - would go to www.test.com

 

 

http://www.test.com/test1 - would go to www.testone.com

 

 

http://www.test.com/test2 - would got to www.testtwo.com

 

 

And any other URI would result in it just going to www.test.com

 

 

Here is the irule I am trying...

 

 

when HTTP_REQUEST {

 

 

Check the requested URI set to lowercase

 

switch -glob "[string tolower [HTTP::uri]]" {

 

 

"/test1" {

 

Requested URI /test1

 

HTTP::respond 301 Location "http://www.testone.com/"

 

}

 

"/test2" {

 

Requested URI /test2

 

HTTP::respond 301 Location "http://www.testtwo.com/"

 

}

 

}

 

if {[HTTP::uri] ne "/"} then {[HTTP::uri] "/"}

 

}

 

 

Any idea what I am doing wrong? I assume it is related to the if statement after the switch, as the switch seems to work.

 

 

Thanks in advance!

 

7 Replies

  • If you want to set the URI you'd use HTTP::uri "/new_uri". The square braces force the interpreter to run HTTP::uri as a command and substitute the value. So you'd get a runtime error with that.

    Also, it would make sense to rewrite the URI only if the first two cases aren't matched. Can you try moving the HTTP::uri command to a default case in the switch statement:

    
    when HTTP_REQUEST {
    
        Check the requested URI set to lowercase
       switch -glob "[string tolower [HTTP::uri]]" {
    
          "/test1" {
              Requested URI /test1
             HTTP::respond 301 Location "http://www.testone.com/"
          }
          "/test2" {
              Requested URI /test2
             HTTP::respond 301 Location "http://www.testtwo.com/"
          }
          default {
             HTTP::uri "/"
          }
       }
    }
    

    Aaron
  • Sorry to drag this old question up again, but say I wanted to preserve the URI and add it to the end of the redirection, how would I do that?

     

     

    For example, I tried:

     

     

    "/uri1" {

     

    Requested URI started with /uri1

     

    HTTP::respond 301 Location "http://www.test.com[HTTP::uri]"

     

     

    It redirects fine, but doesn't re-append the URI.

     

     

    Have I got the speech marks or square brackets wrong?

     

     

    Thanks!

     

  • I just tested this and it worked:

     

    when HTTP_REQUEST {

     

    Check the requested URI set to lowercase

     

    switch -glob "[string tolower [HTTP::uri]]" {

     

    "/uri1" {

     

    HTTP::respond 301 Location "http://www.test.com[HTTP::uri]"

     

    }

     

    default {

     

    HTTP::uri "/"

     

    }

     

    }

     

    }

     

     

    Is there something else going on in your code that may not be represented in your snippet?
  • Thanks for the reply, Ryan.

     

     

    To give you the background, what I am trying to do is redirect based on the presence of a couple of keywords in the URI, but when it does get redirected, to remove the keyword part but preserve the rest of the URI after the redirection.

     

     

    For example, www.test.com/test1/hello would redirect to www.testone.com/hello because test1 is a keyword. I haven't even got to the part where I remove the keyword from the URI though, I can't even preserve the URI after the redirection so far! With the code below, it doesn't append the URI to the new site (www.test.com/test1/hello just goes to www.testone.com, for example).

     

     

    I also added code to ignore the CSS file as the formatting was wrong without it, so I'm not sure if that breaks anything.

     

     

    If a keyword is not in the URI, it should not redirect at all, but should remove the URI so they don't get a "not found" for things that don't exist (hence the default bit and the CSS exception).

     

     

    Here's my code:

     

     

    when HTTP_REQUEST {

     

     

    Check the requested URI set to lowercase

     

    switch -glob "[string tolower [HTTP::uri]]" {

     

     

    "/test1*" {

     

    Requested URI started with /test1

     

    HTTP::respond 301 Location "http://www.testone.com[HTTP::uri]"

     

    }

     

    "/test2*" {

     

    Requested URI started with /test2

     

    HTTP::respond 301 Location "http://www.testtwo.com[HTTP::uri]"

     

    }

     

    "/style.css*" {

     

    return

     

    }

     

    default {

     

    HTTP::uri "/"

     

    }

     

    }

     

     

    }

     

     

    Thanks for any advice!
  • As soon as you do a 301 Redirect the traffic is gone and can no longer be affected by this iRule.

     

     

     

    For example, www.test.com/test1/hello would redirect to www.testone.com/hello because test1 is a keyword. I haven't even got to the part where I remove the keyword from the URI though, I can't even preserve the URI after the redirection so far! With the code below, it doesn't append the URI to the new site (www.test.com/test1/hello just goes to www.testone.com, for example).

     

     

     

    It is going to be impossible to mask a URI on a redirect. You can mask it to the server, but on the initial request it is going to have to be there.