Forum Discussion

Nick_125548's avatar
Nick_125548
Icon for Nimbostratus rankNimbostratus
Sep 11, 2013

rewrite uri with datagroup

Hi

I'm new to irules and have got where I am but looking at other irules we have, but am still struggling.

I have a request from marketing to host a number (40) of small websites. Rather than setting up a new vip/pool/node/website for each one I would like to inspect the hostheader, forward it to a pool, rewrite the uri and be invisible to the user. This way I can simply copy the content to a single website. e.g.

nick.domain.co.uk/* -> forward to pool POC_Boxes -> uri rewritten to /nick/*
alan.domain.co.uk/* -> forward to pool POC_Boxes -> uri rewritten to /alan/*
lara.domain.co.uk/* -> forward to pool POC_Boxes -> uri rewritten to /lara/*

I have been able to get this working using a switch irule but would prefer a datagroup as it is easier to maintain going forward. However, I am having trouble reading data from the datagroup

here is my switch irule

when HTTP_REQUEST {
set marketingURI "[HTTP::uri]" 
switch -glob -- [string tolower [URI::decode [HTTP::host]]] \
    "nick.domain.co.uk" {
    HTTP::uri "/nick$marketingURI"
    pool POC_boxes
    }
switch -glob -- [string tolower [URI::decode [HTTP::host]]] \
    "lara.domain.co.uk" {
    HTTP::uri "/lara$marketingURI"
    pool POC_boxes
    }
switch -glob -- [string tolower [URI::decode [HTTP::host]]] \
    "alan.domain.co.uk" {
    HTTP::uri "/alan$marketingURI"
    pool POC_boxes
    }
else {
    HTTP::redirect "http://www.domain.com"
    }
}

Here is my datagroup irule

when HTTP_REQUEST {
if { [class match [string tolower [HTTP::host]] starts_with POC_DGL_Marketing_trades] } {
    set url_URI [class match -value [string tolower [HTTP::host]] starts_with POC_DGL_Marketing_trades]
    set marketingURI "$url_uri[HTTP::uri]" 
    HTTP::uri "$marketingURI"
    pool POC_boxes
} else {
    HTTP::redirect "http://www.domain.com"
}
log local0. "Marketing Trades [HTTP::host]"

}

Here is my datagroup

Name            Value
nick.domain.co.uk       /nick
alan.domain.co.uk       /alan
lara.domain.co.uk       /lara

Thanks

Nick

  • We do something similar although we don't rewrite the URL, what we do is look up the incoming URL then redirect to a pool but leave the URL alone, our code is:

     

    when HTTP_REQUEST {

     

    Get the requested host header
    set req [HTTP::host]
    get the actual pool based on the header
    set redir [class lookup [HTTP::host] datagroup_name]
    
    check if we have already LB them but looking for our cookie
    
    log the redirect for troubleshooting (enable/disable as needed)
    log local0. "Redirecting request for [HTTP::host] to $redir"
    
    direct them to the pool as normal 
         pool $redir
         eval [LB::select] 
         persist cookie

    }

     

  • The case for 'url_uri' changes between line 3 and 4 which will break things; hopefully this is your issue with the DG based rule.

     

    Lines 4 and 5 could be reduced I think, but it's unclear as you seem to want to retain and modify rather than replace the uri as you state.

     

  • Hi Thanks for the feedback.

     

    I've corrected the case issue and it's still not working. From what i can see the 'if' statement on line 2 isn't looking at the DG as I get redirected to www.domain.com.

     

    doing a redirect will be difficult as marketing have a requirement that all sites have their own hostname. I would still need to modify/replace the uri so that I can direct the traffic to a different folder on the website.

     

    How would I 'replace' the uri instead or retaining/modifying it? Would it be simpler to forward traffic like this nick.domain.co.uk/ -> nick.domain.co.uk/nick/?

     

  • OK, can you double-check the DG name and it's case too please.

     

    Also, if you add 'log local0. "[HTTP::host]"' just before the redirect after the else statement we can also see if the host value is what we think it is.

     

  • Hi

     

    it is working now, after correcting the case issue it looks like I had removed a space between url_uri and [class match ......

     

    Thanks for your help.

     

  • I think you should be using 'starts_with' or 'equals', 'ends_with' could match almost anything. Also, you should be lowering the HTTP::uri as in the DG irule in the original question.