Forum Discussion

wcm's avatar
wcm
Icon for Nimbostratus rankNimbostratus
May 04, 2021

iRule to add ".org" to host and redirect to HTTPS

Hello,

 

I'm trying to write an irule that will take an HTTP request like "http://example" and rewrite it with ".org" or some other suffix.

 

It seems stupid that this is even required, but it is a request that came in and I would like to be able to deliver. Here is what I have so far:

 

when HTTP_REQUEST {

if { [string tolower [HTTP::host]] equals "example" } {

 HTTP::respond 301 Location "https://example.org"

}

}

 

The rewrite seems to be working; it takes http://example and writes it to example.org but it doesn't add HTTPS, so the page fails to load.

 

What would be the best way to take an HTTP request that is missing the .org, .com etc and append it properly, while also redirecting to HTTPS?

 

Thanks

1 Reply

  • If you have a look in the default redirect iRule on the F5 system ( _sys_https_redirect), you can see a nice example of how it roughly could look. If you then add your statement for filtering the correct domain name, as well as the .org-bit, it would look something like this:

     

    when HTTP_REQUEST {

    if { [string tolower [HTTP::host]] equals "example" } {

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

       }

    }

     

    You can remove the HTTP::uri and/or HTTP::host in the redirect and replace with the hardcoded domain name if you want, or you can use this format to keep it more flexible for other uses.

     

    Hope this helps.