Forum Discussion

Andrzej_Waszkie's avatar
Andrzej_Waszkie
Icon for Nimbostratus rankNimbostratus
Mar 25, 2015

URL Append

I'm trying to make a rule for a url to be appended if there is nothing after the root one. Example: and.rew.com if a user comes in with and.rew.com/app1/ or and.rew.com/app2/ and etc nothing is done. However, if he comes in on and.rew.com he gets redirected to and.rew.com/apps/ url

I tried the following as an example but not working the issue is that depending on your login you can be using app1 or app2 and moment it changes to app2 it gets messed up.

when HTTP_REQUEST {

Check if path doesn't already start with /app1/ if { not ([HTTP::path] starts_with "/app1/")}{
    Rewrite path by appending "/app1/" 
   HTTP::path "/apps/[HTTP::path]"  
}  

}

1 Reply

  • Andrzej, the HTTP::path command will change the URL as it's passed back to the backend app server. It doesn't issue a "redirect" back to the client so the browser will still show the original url but the web logs in the backend will show the newly updated value.

     

    If you want the browser to be reflected with the change, you'll want to use "HTTP::redirect" to send a 302 redirect (or HTTP::respond for a 301) to the browser to tell it to issue a new request to the changed URL. Then the browser address bar will be correct with what the backend server receives.

     

    There are reasons for both commands so it really depends on what you want want the user experience to be.

     

    In your example, if the url doesn't start with "/app1/", then it prepends "/apps/" to the URL. it's not "appending" as you note int he comment Here are some examples

     

    Browser -> Server
    http://and.rew.com/app1/ (HTTP::path = /app1/) -> http://and.rew.com/app1/
    http://and.rew.com/ (HTTP::path = /) -> http://and.rew.com/apps//
    http://and.rew.com/foo (HTTP::path = /foo) -> http://and.rew.com/apps//foo
    http://and.rew.com/foo/bar (HTTP::path = /foo/bar) -> http://and.rew.com/apps//foo/bar

    Since HTTP::path will already be prefixed with a "/" and you are prefixing with "/apps/" that has a trailing slash, there will be 2 slashes which probably isn't what you want.

     

    Back to your original question, I'm a little confused. You said for all URLs that are not the root "/", nothing is done but for the root "/" you get redirected to "/apps/". But, your iRule only checks for "/app1/" and modifies all other URLs. In the end of your question you also appended "url" to "/apps/". But in that case, url is only "/".

     

    Per your original question, I'd code the iRule something like this:

     

    when HTTP_REQUEST {
      if { HTTP::path eq "/" } {
        HTTP::path "/apps/"
         or issue a redirect
        HTTP::redirect "/apps/"
      }
    }

    If this doesn't get what you want, if you can provide a "before" and "after" url mapping with several examples, that will help with narrowing down the logic.

     

    -Joe