Forum Discussion

Walter_WorkAcct's avatar
Walter_WorkAcct
Icon for Altostratus rankAltostratus
Dec 13, 2021

iRule to forward to subsites

In the following code, I want test1, test2 and test3 to route to the pool "pool_subsites_8443" and serve pages and all others to simply go to the main website. However, if you put in website/~test4 you simply get a 404. How can I have this go to the main website instead of return a 404?

when CLIENT_ACCEPTED {
    set hsl [HSL::open -publisher /Common/hsl_publisher]
}
when HTTP_REQUEST {
    if { [HTTP::uri] starts_with "/~" } {
       HSL::send $hsl "The URI is [HTTP::uri] contains a tilde and will be sent to staff server."
       switch -glob [HTTP::uri] {
            "/~test1*" -
            "/~test2*" -
            "/~test3*" 
            { 
                # URI ends with a specific username
                # All these cases use the same action.
         pool pool_subsites_8443 
         }
       default {
             pool pool_main_8443
            }
        }
    } else {
        return     
    }
}
when CLIENT_CLOSED {
  unset hsl
}
  • Hi Walter_WorkAcct,

    Can you try this iRule?

    when CLIENT_ACCEPTED {
    	set hsl [HSL::open -publisher /Common/hsl_publisher]
    }
     
    when HTTP_REQUEST {
    	set status 0
    	if { [HTTP::uri] starts_with "/~" } {
    		set status 1
    		HSL::send $hsl "The URI is [HTTP::uri] contains a tilde and will be sent to staff server."
    		switch -glob [HTTP::uri] {
    			"/~test1*" -
    			"/~test2*" -
    			"/~test3*" { 
    				# URI ends with a specific username
    				# All these cases use the same action.
    				pool pool_subsites_8443 
    			}
    			default {
    				pool pool_main_8443
    			}
    		}
    	}
    	else {
    		return     
    	}
    }
     
    when HTTP_RESPONSE {
    	if { $status && [HTTP::status] == 404 } {
    		HTTP::redirect "https://example.com"
    	}
    }
     
    when CLIENT_CLOSED {
    	unset hsl
    }
    • Walter_WorkAcct's avatar
      Walter_WorkAcct
      Icon for Altostratus rankAltostratus

      Initially I thought this fix was working correctly but there is a strange bug that if for example I am in example.com/~test1 and go to example.com/~test9 (test9 exists but is not in the select statement) it doesn't redirect and serves a page that I am not 100% sure where it is coming from. Still troubleshooting.

      It is like it redirects to www.example.com but is serving the data with a subdirectory from this staff server.