Forum Discussion

Tyranon_113005's avatar
Tyranon_113005
Icon for Nimbostratus rankNimbostratus
Jan 15, 2018

HTTP redirect by HOST and URL

Hello,

I'm trying redirect HTTP request depends on HTTP host and url.

Example,

https://www.aaa.com/123 ->   https://www.bbb.com/specified1

https://www.aaa.com/456 ->   https://www.bbb.com/specified2

https://www.aaa.com/* ->   https://www.aaa.com/*

https://www.bbb.com/* ->   https://www.bbb.com/*

Does the below work as I expected?

when HTTP_REQUEST {
  if { [HTTP::host]] equals "www.aaa.com"}{
     switch [HTTP::uri] {
        "/123" { HTTP::redirect "https://www.bbb.com/specified1" }
        "/456" { HTTP::redirect "https://www.bbb.com/specified2" }
        }
        default {
           HTTP::redirect "https://[HTTP::host][HTTP::uri]"
        }
     }
  elseif { [HTTP::host] equals "www.bbb.com" }{
    HTTP::redirect "https://[HTTP::host][HTTP::uri]"
}

}

3 Replies

  • Your code will loop!

     

    If you want the request to be forwarded to the server, don't use redirect command!

     

    when HTTP_REQUEST {
      if { [HTTP::host]] equals "www.aaa.com"}{
         switch [HTTP::uri] {
            "/123" { HTTP::redirect "https://www.bbb.com/specified1" }
            "/456" { HTTP::redirect "https://www.bbb.com/specified2" }
            }
            default {
               forward the request... no action
            }
         }
    }
  • oguzy's avatar
    oguzy
    Icon for Cirrostratus rankCirrostratus

    Hi Yuri,

     

    You can try the below code.

     

    Code

     

    when HTTP_REQUEST {
    
          if { ([HTTP::host] equals "www.aaa.com")}{
               switch [HTTP::uri] {
                  "/123" { HTTP::redirect "https://www.bbb.com/specified1" }
                  "/456" { HTTP::redirect "https://www.bbb.com/specified2" }  
               default {
                 HTTP::redirect "https://www.bbb.com[HTTP::uri]"
               }
            }
        }
      }