Forum Discussion

Joe_Pipitone's avatar
Joe_Pipitone
Icon for Nimbostratus rankNimbostratus
Apr 07, 2014

Various rewrites/redirects on one VIP

I am currently burning up 3 VIPs to get all these redirects and rewrites working, however I'd like to efficiently combine the iRule, and point all 3 domains to the same VIP and accomplish the same thing. Having some issues and there are redirect loops occurring.

I have 2 domains, and 2 subdomains, which I've masked as:

olddomain.com

newdomain.com

newforum.newdomain.com

oldforum.olddomain.com

I need to allow all traffic to newdomain.com, and newforum.newdomain.com. This iRule needs to catch all traffic going to olddomain.com, append the URI, and rewrite to newdomain.com. I also have to catch all forum requests, and rewrite the domain and path. This is already working on a separate VIP. I tried catching both www.olddomain.com and olddomain.com with an asterisk, but it doesn't seem to work. If anyone has any ideas on what I may be missing, please let me know. Thank you!

   Rewrites for 3 separate domains on one VIP.  Also rewrites forum requests from old forum to new forum
    when HTTP_REQUEST {
         This needs to catch oldforum.olddomain.com and rewrite to newforum.newdomain.com
        if { [HTTP::query] contains "TID=" } {
            HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]"
            return
        }
        if { [HTTP::query] contains "FID=" } {
            HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] FID]"
            return
        }
        if { [HTTP::query] contains "C=" } {
            HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] C]"
            return
        }
         This needs to catch newdomain.com and analyze URI
        if { [string tolower [HTTP::uri]] contains "somestring" } {
            HTTP::redirect "http://newdomain.com/some/path/somepage.aspx?eofseckey=233729AF8"
            return
        } 
         This needs to catch newforum.newdomain.com on port 80, append URI, and redirect to port 443
        if { ([HTTP::host] eq "oldforum.olddomain.com") } { 
            switch -glob [HTTP::host] { 
                 "/" { HTTP::redirect "https://newforum.newdomain.com" } 
                 default {  HTTP::redirect "https://newforum.newdomain.com[HTTP::uri]" }
            } 
        } 
         This needs to catch various requests to olddomain.com and rewrite to newdomain.com
        if { ([HTTP::host] contains "olddomain.com") } { 
            switch -glob [HTTP::host] { 
                 "/" { HTTP::redirect "http://newdomain.com" } 
                 default {  HTTP::redirect "http://newdomain.com[HTTP::uri]" }
                 "/oldpath/path/oldpage-2013.aspx" {  
                  HTTP::redirect "http://newdomain.com/new-path/newpage.aspx"
                  return  
               }
               "/faq*" {
                  HTTP::redirect "http://another.domain.com/some/path/FAQ.pdf"
                  return
               }    
            }      
         }
      }

11 Replies

  • you say rewrite in your explanation, but are using redirect in your iRule. Can you clarify what the objective is? Do you want to rewrite (don't change the client experience) or redirect (move clients to new domain...they will see this change in the browser)
  • giltjr's avatar
    giltjr
    Icon for Nimbostratus rankNimbostratus

    I can see why you are getting loops. Take your first if:

    if { [HTTP::query] contains "TID=" } { HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]" return }

    Since you are not checking for a host name it will match both "oldforum.olddomain.com" and "newforum.newdomain.com"

    I would do something like this to start with:

    if {[HOST::name] ends_with "newdomain.com"} {
        Stuff do do when domain is "newdomain.com"
        }
    elseif {[HOST::name] ends_with "olddomain.com"} {
        Stuff to do when domain is "olddomain.com"
        }  else {
        stuff do do in case host name ends with something we did not expect.
        }      
    
  • We are doing a bit of both - rewriting the domain, and redirecting as well.
  • Are you saying I can nest if statements within another if statement?

    if {[HOST::name] ends_with "newdomain.com"} {
        Stuff do do when domain is "newdomain.com"
        }
        if { [HTTP::query] contains "TID=" } {
                HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]"
                return
            }
            if { [HTTP::query] contains "FID=" } {
                HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] FID]"
                return
            }
            if { [HTTP::query] contains "C=" } {
                HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] C]"
                return
            }
           }
           elseif {[HOST::name] ends_with "olddomain.com"} {
        Stuff to do when domain is "olddomain.com"
        }  else {
        stuff do do in case host name ends with something we did not expect.
        }   
    
    • JRahm's avatar
      JRahm
      Icon for Admin rankAdmin
      yep, nesting if statements is absolutely acceptable.
  • Great. One last question - I have used conditional statements before, for example:

    if domain name is domain.com OR domain2.com

    do stuff

    For some reason, this syntax is not being accepted after reviewing logs. Any ideas?

    elseif {[HTTP::host] ends_with "domain.com" - "domain2.com"} {

    • Joe_Pipitone's avatar
      Joe_Pipitone
      Icon for Nimbostratus rankNimbostratus
      Wondering if it may be more efficient to do it this way, rather than inserting another if statement - I have to perform the same redirect for both domains.
  • giltjr's avatar
    giltjr
    Icon for Nimbostratus rankNimbostratus

    I don't think the the statement

     "{[HTTP::host] ends_with "domain.com" - "domain2.com"}" 
    

    Is not valid. Now the following is:

     "{[HTTP::host] ends_with "domain.com" OR [HTTP::host] ends_with "domain2.com"}"
    

    I would have to test you might be able to do:

     "{[HTTP::host] ends_with ("domain.com" OR "domain2.com")}"
    
  • Still having trouble getting this rule to work. The first part works, where it redirects the old forum topics and threads, however it doesn't seem to work its way down and catch newdomain.com or olddomain.com. I setup logging, which is one of the reasons why I know its not getting past the first batch of if statements.

    domain1, domain2, and forum rewrites
    when HTTP_REQUEST {
        if {[HTTP::host] ends_with "oldforum.olddomain.com"} {
             This needs to catch oldforum.olddomain.com and rewrite to newforum.newdomain.com
           if { [HTTP::query] contains "TID=" } {
               HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]"
               return
            }
            if { [HTTP::query] contains "FID=" } {
               HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] FID]"
               return
            }
            if { [HTTP::query] contains "C=" } {
               HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] C]"
               return
            }
            elseif { ([HTTP::host] ends_with "newdomain.com") or ([HTTP::host] ends_with "olddomain.com") } {
             This needs to catch newdomain.com/uri or olddomain.com/uri and redirect  
            log local0. "Hostname is [HTTP::host]" }
            if { [string tolower [HTTP::uri]] contains "secure" } {
               HTTP::redirect "http://newdomain.com/Issues/2014/04/some-page.aspx"
               return
            }
            else {
            if { [string tolower [HTTP::uri]] contains "secure2" } {
               HTTP::redirect "http://newdomain.com/Issues/2014/04/some-other-page.aspx"
               return
           }
        }             
      }
    }
    
  • Small typo I think, but here's another version:

    when HTTP_REQUEST {
        switch -glob [string tolower [HTTP::host]] {
            "*oldforum.olddomain.com" {
                 This needs to catch oldforum.olddomain.com and rewrite to newforum.newdomain.com
                if { [HTTP::query] contains "TID=" } {
                    HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]"
                } elseif { [HTTP::query] contains "FID=" } {
                    HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] FID]"
                } elseif { [HTTP::query] contains "C=" } {
                    HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] C]"
                }
                return
            } 
            "*newdomain.com" -
            "*olddomain.com" {
                 This needs to catch newdomain.com/uri or olddomain.com/uri and redirect  
                if { [string tolower [HTTP::uri]] contains "secure" } {
                    HTTP::redirect "http://newdomain.com/Issues/2014/04/some-page.aspx"
                } elseif { [string tolower [HTTP::uri]] contains "secure2" } {
                    HTTP::redirect "http://newdomain.com/Issues/2014/04/some-other-page.aspx"
                }
            }
        }
    }
    
  • giltjr's avatar
    giltjr
    Icon for Nimbostratus rankNimbostratus

    Try the following. I think you had some { } out of order and you had a "else {if" when a elseif was better.

    domain1, domain2, and forum rewrites
    when HTTP_REQUEST {
        if {[HTTP::host] == "oldforum.olddomain.com"} {
             This needs to catch oldforum.olddomain.com and rewrite to newforum.newdomain.com
           if { [HTTP::query] contains "TID=" } {
               HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=posts&t=[URI::query [HTTP::uri] TID]"
               return
            }
            if { [HTTP::query] contains "FID=" } {
               HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=topics&f=[URI::query [HTTP::uri] FID]"
               return
            }
            if { [HTTP::query] contains "C=" } {
               HTTP::redirect "http://newforum.newdomain.com/default.aspx?g=forum&c=[URI::query [HTTP::uri] C]"
               return
            } 
        } elseif { ([HTTP::host] ends_with "newdomain.com") or ([HTTP::host] ends_with "olddomain.com") } {
             This needs to catch newdomain.com/uri or olddomain.com/uri and redirect  
            log local0. "Hostname is [HTTP::host]"
            if { [string tolower [HTTP::uri]] contains "secure" } {
               HTTP::redirect "http://newdomain.com/Issues/2014/04/some-page.aspx"
               return
            } elseif { [string tolower [HTTP::uri]] contains "secure2" } {
               HTTP::redirect "http://newdomain.com/Issues/2014/04/some-other-page.aspx"
               return
           }
        }             
      }