Forum Discussion

Luca_55898's avatar
Luca_55898
Icon for Nimbostratus rankNimbostratus
Mar 22, 2012

Is this valid? Http:query

I need to redirect the following:

 

 

api.mycompany.com/compute/v1/whatever-gets-typed-next

 

 

to

 

 

api.mycompany.com/compute/v2/whatever-gets-typed-next

 

 

The key is that the 'whetever-gets-typed-next' needs to stay in place when the redirect is issued.

 

 

So i have done this:

 

 

/compute/v1/*" {

 

HTTP::respond 301 Location "https://api.mycompany.com/compute/v2/[HTTP::query]"

 

 

It seems to work, testing shows that it is ok.... but just wanted to confirm this is the correct way to do the redirect

 

2 Replies

  • Here's a breakdown of the common URI commands:

     

     

    http://www.example.com:8080/path/to/file.ext?param1=value1&param2=value2

     

     

    [HTTP::host] = www.example.com:8080

     

    [HTTP::uri] = /path/to/file.ext?param1=value1&param2=value2

     

    [HTTP::path] = /path/to/file.ext

     

    [HTTP::query] = param1=value1&param2=value2

     

     

    [HTTP::query] returns the query string from a URI (everything after the ?). Is your "whatever-gets-typed-next" in the query string or part of the path? If the former, what you have there should work.

     

     

    Aaron
  • I have a feeling you were already attempting to handle this within a switch statement, leading me to be believe you want to be able to handle multiple uris....

     

     

     

    may I suggest something like the following:

     

     

     

    rule replace_uri {

     

    when HTTP_REQUEST {

     

    switch -regexp "[string tolower [HTTP::uri]]" {

     

    "^/compute/v1/" {

     

    set replace_uri [string replace [HTTP::uri] 0 11 "/compute/v2/"]

     

    HTTP::respond 301 Location "http://[HTTP::host]$replace_uri"

     

    }

     

    "^/some/other/uri/you/want/to/replace/" {

     

    set replace_uri [string replace [HTTP::uri] 0 35 "/some/other/target/"]

     

    HTTP::respond 301 Location "http://[HTTP::host]$replace_uri"

     

    }

     

    }

     

    }

     

    }

     

     

     

    this "switches" against the uri to look for a "/compute/v1/" at the beginning of the uri. if it finds it, it creates a variable, replace_uri, where it replaces string indices 0 to 11 (that is "/compute/v1/") with "/compute/v2/". It then redirects to http://[HTTP::host]$replace_uri

     

     

     

    the second conditional "switch" looks for "/some/other/uri/you/want/to/replace/" at the beginning of the uri. if it finds it, it creates a variable, replace_uri, where it replaces string indices 0 35 (that is "/some/other/uri/you/want/to/replace") with "/some/other/target/". It then redirects to http://[HTTP::host]$replace_uri

     

     

     

    if neither conditional is met in the switch statement, then nothing happens