Forum Discussion

AndyCooney_1337's avatar
AndyCooney_1337
Icon for Nimbostratus rankNimbostratus
May 22, 2011

rewrite .com to .org for any domain

We host many of our sites as .org but still register the .com and .net domains. We'd like to have a single VIP that takes any "requested_domain.com" and rewrites it to "requested_domain.org". Currently we use a rule that I found on this site by Denny Payne:

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] contains "mysite.com" }

 

{ HTTP:: redirect http://www.mysite.org }

 

}

 

 

This works great however every time we add another domain we have to modify the iRule. I'd like to create one that works on any request.

 

 

If it were to turn mysite.com into a string that it could strip off the .com from and then add a .org to the end redirecting to the new string it would be perfect however I'm not sure how to manipulate strings like that in the iRules world.

 

 

Thanks,

 

 

Andy

 

  • Use regsub or split/join to manipulate the string.

     

     

    set newDomain [regsub -nocase -- {(com|net)\.*$} [HTTP::host] {org}]

     

    HTTP::redirect "$newDomain[HTTP::uri]"

     

     

    Or something to that effect ;-)
  • I think you could do this with string commands more efficiently:

    when HTTP_REQUEST {
        Check if host doesn't end with .org and has at least one alpha character
       if { not ([string tolower [HTTP::host]] ends_with ".org") and [string match {[a-zA-Z]} [HTTP::host]] }{
           Redirect to the same host with the last three characters removed and org added
          HTTP::redirect "http://[string range [HTTP::host] 0 end-3]org[HTTP::uri]"
       }
    }

    Aaron
  • Yet another way to do it üôÇ

    
    when HTTP_REQUEST {
    if { [string tolower [HTTP::host]] ends_with "com" } {
    set host [string map -nocase {com org} [HTTP::host]]
    HTTP::redirect "http://$host[HTTP::uri]"
    }
    }
    
  • Thank you both so much for your replies.

     

     

    The solution provided by hoolio seemed to not work correctly, perhaps it directed the user in circles because of our setup.

     

     

    The solution from Michael worked just perfectly. I modified it to actually do the same for both .com and .net redirecting to the https .org equivalent.

     

     

    Thanks,

     

     

    Andy
  • Maybe the issue was that I left off the original URI? I edited the example above to add that.

     

     

    Aaron