Learn F5 Technologies, Get Answers & Share Community Solutions Join DevCentral

Filter by:
  • Solution
  • Technology
Answers

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 。

0
Rate this Question

Answers to this Question

placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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

1
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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]"
        }
    }
}
1
Comments on this Answer
Comment made 19-Oct-2016 by denny 11

Yes,this is more exactly.thank you .I will test it.

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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

0
Comments on this Answer
Comment made 19-Oct-2016 by denny 11

thanks ,But how to cut /mvpvote/xxxx to /xxxx

1
Comment made 19-Oct-2016 by CANSTAYN569 177

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

0
placeholder+image
USER ACCEPTED ANSWER & F5 ACCEPTED ANSWER

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"
        }
    }
0