Forum Discussion

kalav's avatar
kalav
Icon for Nimbostratus rankNimbostratus
Jan 26, 2021
Solved

Redirect in iRule based on host and URI

I have a virtual server on port 80 redirecting traffic to another vs on 443 via http 301 responses in an iRule:

if { [HTTP::host] equals "www.myexample.com" or [HTTP::host] equals "myexample.com"} {
  HTTP::respond 301 Location "https://myexample.com[HTTP::uri]" }

A wildcard certificate is in use for the 443 vs, "*.myexample.com".

I've been asked to move a site to the same server, and to do the same thing with a number of subsites/URIs. Not being very familiar with iRules, I have added this to the applied rule:

when HTTP_REQUEST {
 if { [HTTP::host] equals "www.myexample.com" or [HTTP::host] equals "myexample.com"} {
  HTTP::respond 301 Location "https://myexample.com[HTTP::uri]" }
 elseif { [HTTP::host] equals "blog.myexample.com"} {
  HTTP::respond 301 Location "https://myexample.com[HTTP::uri]" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/ddc"}
  HTTP::respond 301 Location "https://uk.myexample.com" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/tree-tales"}
  HTTP::respond 301 Location "https://uk.myexample.com" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/made-here"}
  HTTP::respond 301 Location "https://uk.myexample.com" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/con21-reaction"}
  HTTP::respond 301 Location "https://uk.myexample.com" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/btp"} {
  HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b-2-p/" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/bas"} {
  HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b-a-s" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/tenbelt"} {
  HTTP::respond 301 Location "https://myexample.com/belt" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/urban"} {
  HTTP::respond 301 Location "https://myexample.com/U_L_C" }
 elseif { [HTTP::host] equals "blog.myexample.com" and [HTTP::uri] equals "/bhfl"} {
  HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b_h_f_l" }
}
}

With this in place, I get a correct redirect from:

blog.myexample.com

blog.myexample.com/ddc

blog.myexample.com/tree-tales

blog.myexample.com/made-here

blog.myexample.com/con21-reaction

blog.myexample.com/bhfl

blog.myexample.com/urban

blog.myexample.com/tenbelt

but 

blog.myexample.com/btp returns myexample.com/btp

blog.myexample.com/bas returns myexample.com/bas

As far as I can tell they are the same as the others and the redirect location itself each responds correctly.

Any pointers on why these couple of redirects might be breaking? 

Many thanks

Kev

  • Hi Kev,

     

    The elseif on lines 4-5, must be after the line 23 in your code.

    4. elseif { [HTTP::host] equals "blog.myexample.com"} {

    switch version:

    when HTTP_REQUEST {
    	switch [HTTP::host] {
    		"www.myexample.com" -
    		"myexample.com" { HTTP::respond 301 Location "https://myexample.com[HTTP::uri]"	}
    		"blog.myexample.com" {
    			switch [HTTP::uri] {
    				"/ddc" -
    				"/tree-tales" -
    				"/made-here" -
    				"/con21-reaction" { HTTP::respond 301 Location "https://uk.myexample.com" }
    				"/btp" { HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b-2-p/" }
    				"/bas" { HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b-a-s" }
    				"/tenbelt" { HTTP::respond 301 Location "https://myexample.com/belt" }
    				"/urban" { HTTP::respond 301 Location "https://myexample.com/U_L_C" }
    				"/bhfl" { HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b_h_f_l" }
    				default { HTTP::respond 301 Location "https://myexample.com[HTTP::uri]" }
    			}
    		}
    	}
    }

     

2 Replies

  • Hi Kev,

     

    The elseif on lines 4-5, must be after the line 23 in your code.

    4. elseif { [HTTP::host] equals "blog.myexample.com"} {

    switch version:

    when HTTP_REQUEST {
    	switch [HTTP::host] {
    		"www.myexample.com" -
    		"myexample.com" { HTTP::respond 301 Location "https://myexample.com[HTTP::uri]"	}
    		"blog.myexample.com" {
    			switch [HTTP::uri] {
    				"/ddc" -
    				"/tree-tales" -
    				"/made-here" -
    				"/con21-reaction" { HTTP::respond 301 Location "https://uk.myexample.com" }
    				"/btp" { HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b-2-p/" }
    				"/bas" { HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b-a-s" }
    				"/tenbelt" { HTTP::respond 301 Location "https://myexample.com/belt" }
    				"/urban" { HTTP::respond 301 Location "https://myexample.com/U_L_C" }
    				"/bhfl" { HTTP::respond 301 Location "https://uk.myexample.com/projects/uk/b_h_f_l" }
    				default { HTTP::respond 301 Location "https://myexample.com[HTTP::uri]" }
    			}
    		}
    	}
    }

     

  • kalav's avatar
    kalav
    Icon for Nimbostratus rankNimbostratus

    Thanks Enes,

     

    The first elseif on line 4 and 5 really should be at the end as a default, right. That makes sense.

    I will see how the switch version works as well, get some testing done and get a reply back, but this makes sense so ought to work. Back shortly!

     

    edit: that seems to have sorted me out. Thanks very much for the help!