Forum Discussion

Sokol_69126's avatar
Sokol_69126
Icon for Nimbostratus rankNimbostratus
Apr 29, 2016

Irule forwarding with various options

Hey Guys,

 

I've been asked to create a rule forwarding to various places depending on various requests. the options are as follows:

 

http://www.mysite.com https://www.bettersite.com/mysite/

 

http://www.mysite.com https://www.bettersite.com/mysite/

 

http://www.mysite.com/* https://www.mysite.com/* http://mysite.com https://www.bettersite.com/mysite/

 

http://mysite.com/ https://www.bettersite.com/mysite/

 

http://mysite.com/* https://www.mysite.com/*

 

I created the following and the F5 accepted it but i have a sneaking feeling something is wrong or at least this isn't the best way to do things. Can anyone give me their opinion as to if this works or is there something better?

 

when HTTP_REQUEST { if { [string tolower [HTTP::host]] equals "mysite.com" } { HTTP::redirect https://www.bettersite.com/mysite/ } if { [string tolower [HTTP::host]] equals "www.mysite.com" } { HTTP::redirect https://www.bettersite.com/mysite/ } if { [string tolower [HTTP::host]] contains "mysite.com/" } { HTTP::redirect https://www.bettersite.com/mysite/[HTTP::uri] } if { [string tolower [HTTP::host]] contains "www.mysite.com/" } { HTTP::redirect https://www.bettersite.com/mysite/[HTTP::uri] } else { HTTP::respond 301 Location https://www.mysite.com/[HTTP::uri] return } }

 

3 Replies

  • Hello,

     

    What we done is redirect and not forwarding.

     

    First of for https redirecting you have to create a specific VS with an http profile and irule provide by F5 that allow redirect from http to https. exemple:

     

    VS-mysite-80 --> IP1 (with an irule for https redirect provide by F5 --> _sys_https_redirect + http profile)

     

    VS-mysite-443 --> IP1 (same IP that VS-mysite-80) + Irule that I provide you below

     

    VS-bettersite-80 --> IP2 (with an irule for https redirect provide by F5 --> _sys_https_redirect + http profile)

     

    VS-bettersite-443 --> IP2 (same IP that VS-bettersite-80) + Irule that I provide you below

     

    second time you have to create a specific IRULE for URI and Hostname redirect without taking into account https redirection.

     

    when HTTP_REQUEST {

     

    if { ([HTTP::uri] eq "/") && (([HTTP::host] eq "mysite.com") && ([HTTP::host] eq "www.mysite.com")) } {

     

    HTTP::redirect https://www.bettersite.com/mysite/

     

    }

     

    }

     

    Regards

     

  • Hi,

    instead of multiple if commands, it is recommended to use switch

    when HTTP_REQUEST {
        if { [string tolower [HTTP::host]] equals "mysite.com" } {
            switch -glob [string tolower [HTTP::path]] {
                "/" - 
                "/mysite/" { HTTP::redirect https://www.bettersite.com/mysite/ }
                default {
                    HTTP::redirect https://www.bettersite.com/mysite[HTTP::uri]
                    return
                }
            }
        } else { HTTP::respond 301 Location https://www.mysite.com[HTTP::uri] return }    
    }
    

    when appending [HTTP::uri] to a host, do not add / as [HTTP::uri] already starts with /

  • Hello Sokol, Now I understand your need.

     

    So to maitain the uri you have to use the follwing irule:

     

    when HTTP_REQUEST {

     

    if { ([HTTP::uri] eq "/") && (([HTTP::host] eq "mysite.com") || ([HTTP::host] eq "www.mysite.com")) } {

     

    HTTP::redirect https://www.bettersite.com/mysite/

     

    } else {

     

    HTTP::redirect https://www.bettersite.com[HTTP::uri]

     

    }

     

    }

     

    or to do more simple:

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] eq "/" } {

     

    HTTP::redirect https://www.bettersite.com/mysite/

     

    } else {

     

    HTTP::redirect https://www.bettersite.com[HTTP::uri]

     

    }

     

    }

     

    Regards