Forum Discussion

Randy_Moran_110's avatar
Randy_Moran_110
Icon for Nimbostratus rankNimbostratus
Feb 27, 2008

http-to-https redirect problem

Hi all,

 

 

I'm trying to get the following to work. If the client is asking for "http://portal.innovation.net", he's directed to "https://portal.innovation.net/PortalWAR/appmanager/portal/home"

 

 

If he's asking for "http://portal.innovation.net/blah/blah", he's directed to https://portal.innovation.net/blah/blah"

 

 

In other words, if he's asking for an explicit path, he's redirected to his path via https. If he's not asking for a path, he's directed to https://portal.innovation.net/PortalWAR/appmanager/portal/home.

 

 

 

 

when HTTP_REQUEST {

 

 

if {[HTTP::uri "http://portal.innovation.net"]}{

 

 

HTTP::redirect "https://[HTTP::host]:443/PortalWAR/appmanager/portal/home"

 

 

} else {

 

 

HTTP::redirect "https://[HTTP::host]:443/[HTTP::uri]"

 

}

 

 

}

 

 

Is there some error in this rule that would cause it not to work? When I apply it, I get a host unaccessible error.

10 Replies

  • Try this

    
    when HTTP_REQUEST {
    if {[HTTP::host equals "portal.innovation.net"]} {
        HTTP::redirect "https://[HTTP::host]/PortalWAR/appmanager/portal/home"
    } else {
        HTTP::redirect "https://[HTTP::host]/[HTTP::uri]"
      }
    }

    I would also look into the switch statement which executes a bit faster then IF-ELSE
  • Thanks for answering my post, cmbhatt. I really appreciate it. The one thing I see might be a problem with your solution is that in my problem, in both cases, the host is the same. I mean that the first part of the url will be the same in both requests; it's only a matter of if the user is asking for an explicit path after or not. I'll try it and see what happens.
  • Here is a better piece that is more specific to what the user enters, rather then using IF-ELSE statement i used the Switch statement.

     
    when HTTP_REQUEST {
      switch [HTTP::URI] {
         "portal.innovation.net" {
               switch [HTTP::uri] {
    "/" { HTTP::redirect "https://[HTTP::host]/PortalWAR/appmanager/portal/home" }
            "/blah/blah" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
                }
           }
       }
    }

    For more information about switch please take a look a the following link

    http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=129

    Hope this helps.

  • Here is a better piece that is more specific to what the user enters, rather then using IF-ELSE statement i used the Switch statement.

     
    when HTTP_REQUEST {
      switch [HTTP::URI] {
         "portal.innovation.net" {
               switch [HTTP::uri] {
    "/" { HTTP::redirect "https://[HTTP::host]/PortalWAR/appmanager/portal/home" }
             "/blah/blah" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
                }
           }
       }
    }

    For more information about switch please take a look a the following link

    http://devcentral.f5.com/Default.aspx?tabid=63&articleType=ArticleView&articleId=129

    Hope this helps.
  • Yeah that seems to do what I need. Thanks a lot for the help. One last question concerning this rule: If i want to say, basically, "if there is anything after the host name, redirect it to the hostname and whatever follows in the uri," how would I do that?

     

     

     

    when HTTP_REQUEST {

     

    switch [HTTP::host] {

     

    "portal.innovation.net" {

     

    switch [HTTP::uri] {

     

    "/" { HTTP::redirect "https://portal.innovation.net/PortalWAR/appmanager/portal/home" }

     

    "/ProducerWAR/producer?wsdl" { HTTP::redirect "https://[HTTP::host][HTTP::uri]" }

     

    }

     

    }

     

    }

     

    }

     

     

    Where it's bolded is what I'm talking about. Is there some kind of "wildcard" character I can use instead of an explicit uri?
  • Yes there are things you can use to match a pattern

    * Matches any sequence of characters in string, including a null string.

    ? Matches any single character in string.

    [chars] Matches any character in the set given by chars. If a sequence of the form x-y appears in chars, then any character between x and y, inclusive, will match. When used with -nocase, the end points of the range are converted to lower case first. Whereas {[A-z]} matches '_' when matching case-sensitively ('_' falls between the 'Z' and 'a'), with -nocase this is considered like {[A-Za-z]} (and probably what was meant in the first place).

    Here is a small example of that.

     
    when HTTP_REQUEST {
      switch -glob [HTTP::URI] {
         "portal.innovation.net" {
               switch [HTTP::uri] {
    "/" { HTTP::redirect "https://[HTTP::host]/PortalWAR/appmanager/portal/home" }
                   "/blah/blah*" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
                   "*blah" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
                   "blah*" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
                   "b?ah" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
                }
           }
       }
    }
  • hi,

     

     

    i think you forgot the -glob option in the second switch

     

     

    And i'm not sure it really makes sense

     

     

    isn't it HTTP::host for the first switch command ?

     

     

    I know it's just an example but still good to have it right in case someone try to copy/paste it
  • Posted By nmenant on 02/28/2008 10:24 AM

     

     

    isn't it HTTP::host for the first switch command ?

     

     

    I know it's just an example but still good to have it right in case someone try to copy/paste it

     

     

     

    Yes, the URI is not "portal.innovation.net", that's the HTTP::host. The URI in that case is "/". URI should not be confused with URL.

     

     

    Denny

     

     

  • Here it is corrected

    
    when HTTP_REQUEST {  
      switch -glob [HTTP::host] {     
       "portal.innovation.net" {
         switch -glob [HTTP::uri] {
          "/" { HTTP::redirect "https://HTTP::host]/PortalWAR/appmanager/portal/home" }
          "/blah/blah*" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
          "*blah" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
          "blah*" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
          "b?ah" { HTTP::redirect "https://[HTTP::host]/[HTTP::uri]" }
         }       
       }   
      }
    }