Forum Discussion

Phil_Rudich_735's avatar
Phil_Rudich_735
Icon for Nimbostratus rankNimbostratus
Nov 07, 2007

URI Rewrite

Hey all! I'm new to F5 and iRules and need some assistance.

 

 

Looking to take an original URL and perform a rewrite to another site so that the client does not know the true URL address. This is what I have so far:

 

 

when HTTP_REQUEST {

 

switch [HTTP::host] {

 

"www.yyy.mycompany.com" {

 

HTTP::header replace "Host" "zzz.mycompany.com"

 

HTTP::uri "/AAEP/"

 

}

 

}

 

}

 

 

However, there are two issues that I'm running into with this code:

 

Upon the first hit in the browser, it comes up with "Page cannot be displayed" error. Once I refresh, it works... but the other issue is that the graphics on the page do not display correctly.

 

 

Any help for this novice would greatly be appreciated!!

 

 

Thanks,

 

Phil

6 Replies

  • The main issue I can see is that you are rewriting ALL uri's to "/AAEP/". All requests into the site (including image requests) will be rewritten.

    http://www.yyy.mycompany.com/ -> http://zzz.mycompany.com/AAEP/

    http://www.yyy.mycompany.com/foobar -> http://zzz.mycompany.com/AAEP/

    http://www.yyy.mycompany.com/image.gif -> http://zzz.mycompany.com/AAEP/

    ...

    Obviously not what you want to do. Since I have no background on your application, only you can figure out which URI's you need to rewrite. But, odds are you will have to do some sort of conditional test on the value of HTTP::uri before rewriting the URI. With something like the following.

    when HTTP_REQUEST {
      switch [HTTP::host] {
        "www.yyy.mycompany.com" {
          HTTP::header replace "Host" "zzz.mycompany.com"
          switch -glob [HTTP::uri] {
            "/" -
            "/index.html" - 
            "/default.aspx" {
              HTTP::uri "/AAEP/"
            }
          }
        }
      }
    }

    One more thing, if you are only comparing one value for the host, an IF statement might be easier to read. There isn't much performance gain on a switch when you are only doing one comparison.

    when HTTP_REQUEST {
      if { [HTTP::host] equals "www.yyy.mycompany.com" } {
        HTTP::header replace "Host" "zzz.mycompany.com"
        switch -glob [HTTP::uri] {
          "/" -
          "/index.html" - 
          "/default.aspx" {
            HTTP::uri "/AAEP/"
          }
        }
      }
    }

    Hope this helps...

    -Joe
  • Thanks for the response Joe.

    We're close, but no cigar just yet! My "images" fall under the "AAEP" directory, but don't display on the page. When I do a properties on them, they are showing up at the root of the [host] under Images (eg. yyy.mycompany.com/images/picture.jpg instead of yyy.mycompany.com/AAEP/images/picture.jpg) Hope this makes sense.

    I tried your suggestion with a slight modification, but can't seem to tweak it to work:

    when HTTP_REQUEST {
      if { [HTTP::host] equals "wwwcert.abms.benelogic.com"} {
        HTTP::header replace "Host" "memberc06.benelogic.com"
        switch -glob [HTTP::uri] {
          "/" -
          "images/" - 
          "bin/" {
            HTTP::uri "/AAEP/"
          }
        }
      }
    }

    Any thoughts?
  • You are still rewriting all URIs to "/AEEP/"

    http://www.cert.abms.benelogic.com/ -> http://memberc06.benelogic.com/AAEP/

    http://www.cert.abms.benelogic.com/images/picture.jpg -> http://memberc06.benelogic.com/AAEP/

    http://www.cert.abms.benelogic.com/bin/ -> http://memberc06.benelogic.com/AAEP/

    ...

    All requests are going to the backend server as "/AAEP/" which will obviously not work.

    If you can provide me a full list of URL mappings you want to perform, then we can get going. I'm just guessing at this point.

    Something like this would work

    Host: wwwcert.abms.benelogic.com -> memberc06.benelogic.com

    URIs:

    / -> /AEEP/

    /index.html -> /AEEP/index.html

    /images/* -> /AAEP/images/*

    /bin/* -> /AEEP/bin/*

    ...

    I think that mapping is what you are getting at, but it's a bit unclear.

    If those are the mappings you are looking for, this should work.

    when HTTP_REQUEST {
      if { [HTTP::host] equals "wwwcert.abms.benelogic.com"} {
        HTTP::header replace "Host" "memberc06.benelogic.com"
        switch -glob [HTTP::uri] {
          "/" -
          "/images/*" - 
          "/bin/*" {
            HTTP::uri "/AAEP[HTTP::uri]"
          }
        }
      }
    }

    This will basically map a default request "/" or any requests in the /images or /bin directory to be prefixed with "/AAEP". If this isn't what you want, please add a detailed mapping of the transforms you want and we'll see what we can do to help out.

    -Joe
  • You are a genius Joe! That worked!! Looked like I was just missing the * wildcard from the code. (And yes, those were the only two directories - good call).

     

     

    Now, my only last "small" issue is that whenever a browser if first opened and I goto the web address, it comes up with "Cannot Display the Webpage". When I perform a refresh it works fine. Any idea why this is happening?
  • Deb_Allen_18's avatar
    Deb_Allen_18
    Historic F5 Account
    To be honest, the most common cause for that is because there are 2 servers in the pool, but 1 is not responding (and not marked down by an appropriate monitor).

     

     

    If you don't have persistence set on the VS, it might also explain why the graphics don't display as expected -- some loading & some not?

     

     

    HTH

     

    /deb

     

     

     

  • Thanks Deb, you rule... that was exactly the issue... I had 2 servers in the pool and one was not in use.

     

     

    Phil