Forum Discussion

Puneet_73909's avatar
Puneet_73909
Icon for Nimbostratus rankNimbostratus
May 11, 2009

Redirection iRule

Hi,

 

I need help in writing an iRule. I want to read domain name and then redirect it to specific URL.

 

For example if HHTP::request is for www.domain.com then it will redirect it to www.abc.com and if request is for www1.domain.com then it will redirect it to www.xxyz.com.

 

In addition to this if request is for http://www.DomainName.com/doc redirects to http://www.DomainName.com/ecom/pages/nm/nmhomepage.jsp and same goes for

 

http://www1.DomainName.com/minisite redirects to http://www1.DomainName.com/ecom/pages/nm/nmhomepage.jsp

 

 

All requests to http://www.joinDomainName.com gets forwarded to http://www.joinDomainName.com and http://www1.joinDomainName.com gets forwarded to http://www1.joinDomainName.com

 

 

 

Please help!!

 

 

 

Regards,

 

Puneet Khanna

4 Replies

  • Hi Puneet,

    I believe you can write your logic in the following manner

       
     when HTTP_REQUEST { 
     set h [HTTP::host] 
     set u [HTTP::uri] 
     switch -glob $h { 
     "www.domain.com" { 
     HTTP::redirect "http://www.abc.com" 
     } 
     "www1.domain.com" { 
     HTTP::redirect "http://www.xxyz.com" 
     } 
     "www.domainname.com" - 
     "www1.domainname.com" { 
     switch -glob $u { 
     "/doc" - 
     "/minisite" { 
     HTTP::redirect "http://[HTTP::host]/econ/pages/nm/nmhomepage.jsp" 
     } 
     } 
     } 
     } 
      
     } 
     

    Click here to look at the reference to using switch command

    However, I am not exactly sure what you mean about your comment

    "All requests to http://www.joinDomainName.com gets forwarded to http://www.joinDomainName.com and http://www1.joinDomainName.com gets forwarded to http://www1.joinDomainName.com"

    Hope this helps

    CB
  • Thnx CB..

     

     

    What I mean to say is the way I am matching "www" and redirecting it to some url and "www1" to some url. I want the redirection if I get www.joinxyz.com (join keyword in domain name) to www.joinxyz.com/doc/index.html and same goes for www1.joinxyz.com to www1.joinxyz.com/doc/index.html.

     

     

    Given below is the iRULE which wrote but I have to add the JOIN keyword rule in this...

     

     

    when HTTP_REQUEST {

     

    Check if requested host start with www.example.com

     

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

     

    HTTP::redirect "http://www.DomainName.com/nmhomepage.jsp"}

     

    elseif {[string tolower [HTTP::host]] starts_with "www1."} {

     

    Replace the host header value with www1.example.com

     

    HTTP::redirect "http://www1.DomainName.com/nmhomepage.jsp"}

     

    elseif {([HTTP::uri] starts_with "doc" or "minisite" or "fslink" or "ms" or "link" or "null-request" or "exttracking.dyn")} {

     

    Redirection

     

    HTTP::redirect "http://[HTTP::host]/nmhomepage.jsp"}

     

    }

     

    Will this work????

     

  • I think it will work. But I am not sure if this is the most efficient way to have request run through

     

     

    Here is how I look at this code. Let us know if that is what you were intending:

     

    The first conditional statement will check the host name www.example.com and if it matches then redirect and exit out of the event. If it does not then go to the second elseif statement. If the domain starts with www1 then redirect and exit out of the event. This is equivalent to www1* wildcard so pretty much any domain starting with www1 will match this statement. If that doesn't match then it will go to the 3rd elseif. At this point it will not care which domain(except for whatever matches in the previous conditional statement) as so long as it matches starts with whatever is in the URI for example it can be /document or /minisites or /msconfig. As you can see it just needs to match the STARTING letters. This is something you might want to consider because it promotes bad entries. Otherwise if it matches it does a redirect and then exists out of the event.

     

     

    I hope this helps you out.

     

     

    CB
  • CM you are right!!!

     

     

     

    This is what I intented top do..I hope this rule will work and also I need to add LB_failed in this so that if my webserver goes unreachable then it will get redirected to maintainance paige. I am unable to do so.

     

     

    So I need to add another rule saying.....

     

     

    when LB_FAILED {

     

    if {[active_members [LB::server pool]] > 0 } {

     

    Active Pool

     

    pool "Vs_Server"}

     

    else {HTTP::redirect "http://www1.DomainName.com/ecom/pages/nm/nmhomepage.jsp"}

     

    Fallback path

     

    }

     

     

    I just want to add this is my previous rule. Is it possible or I have to create another seperate rule and refer it in same Virtual Server. Will it be a best practice to do refer two seperate iRules in same Virtual Server??