Forum Discussion

Glenn_Ruffus_10's avatar
Glenn_Ruffus_10
Icon for Nimbostratus rankNimbostratus
Jan 07, 2008

Force www in redirect if missing

I asked for and received help a couple of weeks ago regarding an iRule for forcing traffic on an *HTTPS* virtual server to include "www." at the beginning of the URL so that the requested URL would match the URL on the digital certificate. Here is the iRule that was suggested:

 

 

when HTTP_REQUEST {

 

if { not ( [HTTP::host] starts_with "www." ) } {

 

HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"

 

}

 

}

 

 

We found that this could not work on an HTTPS virtual server because I needed the redirect to occur before the traffic was unencrypted, which wasn't possible. The SSL negotiation includes comparing the requested URL with the URL on the certificate. The security warning would occur as a result. This would happen before the redirect could be envoked. So, I gave up on trying to do this on the HTTPS virtual server.

 

 

Now, I need to make it happen on the HTTP virtual server.

 

 

Our existing "redirect to HTTPS" iRule looks like this:

 

 

when HTTP_REQUEST {

 

HTTP::redirect https://[getfield [HTTP::host] ":" 1][HTTP::uri]

 

}

 

 

I would like to combing the logic of the two iRules so that when I attach the iRule to the HTTP virtual server, it will look at the requested URL to see if it has the www. in the URL. If it does, then redirect to HTTPS with the existing URL/URI. If it does not have the www. at the beginning of the URL, then add it to the front of the URL and send to the adjusted URL/URI.

 

 

The first iRule above comes very close. It just doesn't have the logic on what to do with the request if the www. *is* there already.

 

 

It might be something like: if the requested URL starts with www., then redirect the existing URL/URI to HTTPS, but if the requested URL does not start with www., then add the www. and redirect to HTTPS.

 

 

Thanks for the help!

 

 

Glenn

 

 

5 Replies

  • Like this?

    
    when HTTP_REQUEST {
       if {[HTTP::host] starts_with "www."} {
          HTTP::redirect "https://[HTTP::host][HTTP::uri]"
       } else {
          HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"
       }
    }

    If there is a chance clients could make a request with the port number in the request (www.example.com:80), you could strip it out with getfield:

    
    when HTTP_REQUEST {
       if {[HTTP::host] starts_with "www."} {
          HTTP::redirect "https://[getfield [HTTP::host] : 1][HTTP::uri]"
       } else {
          HTTP::redirect "https://www.[getfield [HTTP::host] : 1][HTTP::uri]"
       }
    }

    Aaron
  • Thanks for the help, Aaron! We've been testing with the iRule that you suggested and it seems to be working very well. I really appreciate your assistance!

     

     

    Glenn

     

  • were you able to attach same iRule to HTTPS virtual server?

     

     

    I am getting https profile required for this irule error, when i am trying to attach same rule to https virtual server.

     

     

    Thanks in advance for your comments.

     

     

    Regards,

     

    Yashodeep

     

     

  • Hi,

     

     

    To attach such an iRule on a HTTPS virtual server you need the BIGIP to be the SSL termination for the HTTPS flow.