Forum Discussion

Puli's avatar
Puli
Icon for Nimbostratus rankNimbostratus
Aug 17, 2010

re-direct a URL but maintain the same URL in browser

i have a requirement as below.

 

 

From URL : http://serverA.com/eng/test.hml

 

 

This need to be re-directed or re-written to

 

 

To : http://serverA.com/global/eng/test.html

 

 

The client browser should not show the re-directed url, it should show the original url (serverA.com/eng/test/html) but re-written to serverA.com/global/eng/test.html

 

 

Tried to replace host with serverA.com/global but does not work.

 

 

Any help is appreciated.

 

thanks,

 

Puli.

 

9 Replies

  • There are two ways to change the URL. The first is issuing a redirect to the client that tells the browser to make a new request to the new URL. This will update the browser's address bar with the new URL which isn't what you want.

    The other option is to change the requested URI in the iRule before it's sent to the backend web server. This can be done with the HTTP::uri command. Something like this should do what you want

    when HTTP_REQUEST {
      if { [HTTP::uri] eq "/eng/test.html" } {
        HTTP::uri "/global/eng/test.html"
      }
    }

    The backend server will think the request came in for "/global/eng/test.html" when it really came in as "/eng/test.html".

    Hope this helps...

    -Joe

  • Puli's avatar
    Puli
    Icon for Nimbostratus rankNimbostratus
    thanks joe.

     

     

    Its working.

     

    One intresting thing i noticed is

     

     

    This works as expected, re-directs and browser URL does not change.

     

    HTTP::uri "/global[HTTP::uri]"

     

    use pool serverA_pool

     

     

    However, if i get the path like below ,

     

     

    set myPath [URI::path [HTTP::uri]]

     

    HTTP::uri "/ocomglobal$myPath"

     

    use pool serverA_pool

     

     

    this does not work, it re-directs but changes the browser URL.

     

     

    Just an observation, not sure what the explanation might be.

     

     

    thanks again.

     

     

  • Puli's avatar
    Puli
    Icon for Nimbostratus rankNimbostratus
    Sorry Joe,

     

     

    stuck on another requirement,

     

     

    Above is working if a use types in the entire URL, but if a user types in just the folder path like

     

    http://serverA.com/eng

     

    Below is my scenario.

     

     

    If a user enter http://serverA.com/eng or http://serverA.com/eng/

     

     

    It currently redirects as http://serverA.com/global/eng/index.html

     

     

    I need to re-direct but have the browser display without global in the URL, as below

     

     

    http://serverA.com/eng/index.html

     

     

    Is this possible?

     

     

    appreciate your help.

     

     

     

  • Hamish's avatar
    Hamish
    Icon for Cirrocumulus rankCirrocumulus
    That's because Joes example doesn't do a redirect. It simply changes the URL that the backend sees. What you're doing is the same. But if the user types in a URL that then gets mapped to a DIRECTORY on the backend the backend WILL send a redirect. And the URL will of course change on the browser.

     

     

    What you need to do is put a lot more intelligence in your iRule. Either maintain a list of valid documents that can be accessed without a redirect (And translate to a document instead of a directory), or have a much more complex iRule that intercepts the redirect, and then re-issues the request with the redirected document instead of the directory directly from the F5 without sending the redirect to the browser.

     

     

    H
  • Hi Puli,

    You can then switch fF statement to a switch statement.

    
    when HTTP_REQUEST {
       switch [HTTP::uri] {
       "/eng" - 
       "/eng/" { 
         HTTP::uri "/eng/index.html" 
                  pool serverB_pool
                   }
       "/eng/test.html" { 
          "HTTP::uri "/global/eng/test.html" 
                 pool serverA_pool
                  }
       }
    }
    
    

    I hope this helps

    Bhattman
  • Puli's avatar
    Puli
    Icon for Nimbostratus rankNimbostratus
    Thanks Bhattman,

     

     

    how i use it for all urls.

     

    I'll have many URL and all need to to have global removed from the URL.

     

    How Can i generilize it for all the URL.

     

     

    Can i use in HTTP_RESPONSE

     

    to strip out global when response is sent back to browser,

     

    Do you think that'sll work.

     

     

    appreciate it
  • Hi Puli,

    If you have a lot of URLs then I suppose you might have to use the Class objects.

    If you want to simply strip out global in the URI then I suppose you can use a string map which would search and replace "/global/" with "/"

    
    when HTTP_REQUEST {   
           HTTP::uri [string map {/global/ / } [HTTP::uri]]
    }
    

    I hope this helps

    Bhattman

    Here is an forumn post that talks about it

  • Puli,

     

     

    One other thing. I think the reason you are seeing the uri change in the browser is because even though you are rewriting the request, you are NOT rewriting the responses. Look at the HTTP headers (using something like LiveHTTP Headers for FireFox) and you will see that the responses from the server are sending your browser to the new uri WITH the /global. Try this:

     

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] starts_with "/eng"}{

     

    HTTP::path "/global[HTTP::path]"

     

    }

     

    }

     

     

    when HTTP_RESPONSE {

     

    Remove inserted path from HTTP responses

     

    if { [HTTP::is_redirect]}{

     

    HTTP::header replace Location [string map -nocase {"/global" ""} [HTTP::header Location]]

     

    } elseif {

     

    [HTTP::header value "Content-Type"] contains "text"} {

     

    STREAM::expression "@/global@@"

     

    STREAM::enable

     

    } else {

     

    STREAM::disable

     

    }

     

    }

     

     

     

    This will insert /global to each of the requests and remove it from the responses. You will need to add a generic stream profile to the VIP but I think it will give you what you are looking for.

     

     

    Joe Martin
  • Puli's avatar
    Puli
    Icon for Nimbostratus rankNimbostratus
    Hi Joe,

     

     

    the above code will only remove /global from the response URI right?, it will leave any references in content of the page.

     

    Just confirming.

     

     

    thanks,

     

    Puli.