Forum Discussion

DM_5174's avatar
DM_5174
Icon for Nimbostratus rankNimbostratus
Feb 08, 2011

HTTP REDIRECT AND KEEPING URI AFTER DOMAIN NAME

Hi All,

 

 

I was wondering if I can get a little help here. I have an existing irule that will redirect and add the "www" if

 

users forgets to put this in...so if they type "http://mysite.com", it will redirect them to "http://www.mysite.com".

 

 

The question here is, I have two additional link that was recently added, so when users add "/app1" or "/app2"

 

it does not keep this URI but just redirect them back to "www.mysite.com".

 

 

What we want to accomplish is: (The URI should be a variable, so this can be anything after www.mysite.com/...."

 

 

user enters: http://mysite.com/app1 should get redirect to: http://www.mysite.com/app1

 

 

Should NOT happen (URI "app1" is lost or any variable in the future): http://mysite.com/app1 --> http://www.mysite.com

 

 

In the future the "/app1" can be anything...we would like to keep this like http://www.mysite.com/$variable

 

 

Also, keep existing functionality when user enters "http://mysite.com" --> STILL REDIRECTS TO --> http://www.mysite.com

 

 

 

Thanks for everyone's help.

 

 

 

 

EXISTING IRULE

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] equals "mysite.com" } {

 

HTTP::redirect "http://www.mysite.com"

 

} else {

 

pool WEB_HTTP_POOL1

 

}

 

}

 

 

 

 

3 Replies

  • George_Watkins_'s avatar
    George_Watkins_
    Historic F5 Account
    Hi DM,

    You should be able to just append [HTTP::path] to the redirect URL. Maybe something like this:

    when HTTP_REQUEST {
    if { [HTTP::host] equals "mysite.com" } {
    HTTP::redirect "http://www.mysite.com[HTTP::path]"
    } else {
    pool WEB_HTTP_POOL1
    }
    }

    You can get much more elaborate, but that should solve the issue at hand.

    -George
  • Hi George,

     

     

    After trying this, the redirection did not add the "www" as before.

     

     

    So when I enter http://mysite.com/app1 --> It goes to http://mysite.com/app1 instead of --> http://www.mysite.com/app1

     

     

    Thanks,

     

    -DM

     

     

  • George's iRule should definitely redirect requests with a hostname of mysite.com to www.mysite.com with the original path appended to the redirect. You might want to change [HTTP::path] to [HTTP::uri] to preserve any query string the client uses. If this isn't working for you, try adding debug logging to the iRule to see what the client is requesting. Note I've left off the else clause as the default will be to use the virtual server's default pool.

    
    when HTTP_REQUEST {
       log local0. "[IP::client_addr]:[TCP::client_port]: [HTTP::method] request to [HTTP::host][HTTP::uri]"
       if { [HTTP::host] equals "mysite.com" } {
          log local0. "[IP::client_addr]:[TCP::client_port]: Redirecting to http://www.mysite.com[HTTP::uri]"
          HTTP::redirect "http://www.mysite.com[HTTP::uri]"
       }
    }
    

    Aaron