Forum Discussion

denny_65237's avatar
denny_65237
Icon for Nimbostratus rankNimbostratus
Oct 19, 2016

HTTP Redirect and cut out URL

client request contain url "mvpvote",for example,request http://www.test.com/mvpvote/xxxxx,I want redirect URL to http://www.test.com:8080/xxxxx, I don't how 。

 

12 Replies

  • when HTTP_REQUEST {

     

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

     

    pool Pool_Test.com:8080

     

    }

     

    }

     

    So i would create a new pool that has the same servers but the port is 8080. I would redirect the requests to that pool. Also since i did not modify the full uri, you would need to change the path to content. (You would need to put it below mvpnote folder)

     

    Regards

     

    • CANSTAYN569_243's avatar
      CANSTAYN569_243
      Icon for Nimbostratus rankNimbostratus

      As a quick solution, instead of cutting the url i would change the Path of content under iis or whatever web server you are using. I would add another folder named mvpvote

       

  • when HTTP_REQUEST {

     

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

     

    pool Pool_Test.com:8080

     

    }

     

    }

     

    So i would create a new pool that has the same servers but the port is 8080. I would redirect the requests to that pool. Also since i did not modify the full uri, you would need to change the path to content. (You would need to put it below mvpnote folder)

     

    Regards

     

    • CANSTAYN569's avatar
      CANSTAYN569
      Icon for Nimbostratus rankNimbostratus

      As a quick solution, instead of cutting the url i would change the Path of content under iis or whatever web server you are using. I would add another folder named mvpvote

       

  • Try something like below one..

    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] starts_with "/mvpvote"}{
            set newuri [string map -nocase {"/mvpvote" ""}[HTTP::uri]]
            HTTP::redirect "http://[HTTP::Host]:8080$newuri"
            }
        }
    

    -Jinshu

  • Jinshu irule will work great but if you want the user to see the new url and add a redirect code then you might have to add a few things to it.

     

    when HTTP_REQUEST {
        if {[string tolower [HTTP::uri]] starts_with "/mvpvote"}{
            set newuri [string map -nocase {"/mvpvote" ""}[HTTP::uri]]
            HTTP::redirect "http://[HTTP::Host]$newuri"
            }
        }
  • There are two drawbacks with the use of

    string map
    , in this case. Firstly, it will replaces all instances of "/mvpvote", even if it happens more than once in the path (or if you're using
    HTTP::uri
    , in the entire length of the Request Target). Secondly, it would replace, for example, "/mvpvote2/" with "2/".

    The following solution should be more correct, and as a bonus, better performing (though not tested!):

    when HTTP_REQUEST {
        switch -glob [HTTP::path] {
            "/mvpvote" {
                HTTP::respond 301 Location "http://[HTTP::host]/[HTTP::query]"
            }
            
            "/mvpvote/*" {
                HTTP::respond 301 Location "http://[HTTP::host][substr [HTTP::uri] 8]"
            }
        }
    }
    
  • Vernon_97235's avatar
    Vernon_97235
    Historic F5 Account

    There are two drawbacks with the use of

    string map
    , in this case. Firstly, it will replaces all instances of "/mvpvote", even if it happens more than once in the path (or if you're using
    HTTP::uri
    , in the entire length of the Request Target). Secondly, it would replace, for example, "/mvpvote2/" with "2/".

    The following solution should be more correct, and as a bonus, better performing (though not tested!):

    when HTTP_REQUEST {
        switch -glob [HTTP::path] {
            "/mvpvote" {
                HTTP::respond 301 Location "http://[HTTP::host]/[HTTP::query]"
            }
            
            "/mvpvote/*" {
                HTTP::respond 301 Location "http://[HTTP::host][substr [HTTP::uri] 8]"
            }
        }
    }