Forum Discussion

smeisenzahl's avatar
smeisenzahl
Icon for Employee rankEmployee
Dec 13, 2012

Irule simple redirect or not?

I have an issue where I think I need a redirect but I'm not sure

 

I have a site http://www.testsite.com that is in production and I can't modify the vs at this time. When users type www.testsite.com in the browser they get to the site fine, but when they type in the browser http://testsite.com I get a 404.

 

So, because I can't modify the current vs at this time I have created a new vs on my ltm and am trying to create an irule that redirects to the prod vs, if this is not the correct thing to do could someone help with the correct option, redirect, insert www or something else.

 

I have tried the following rules

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] eq "http://testsite.com" } {

 

HTTP::redirect "http://www.testsite.com"

 

}

 

}

 

 

when HTTP_REQUEST {

 

if { [HTTP::host] equals "testsite.com" }{

 

HTTP::header replace Host "www.testsite.com"

 

}

 

}

 

 

I even tried this

 

when HTTP_RESPONSE {

 

if { [HTTP::status] == 404} {

 

HTTP::redirect "http://www.testsite.com"

 

}

 

}

 

 

or am I way off because non of the above seem to do what I need.

 

 

10 Replies

  • This is what we do, we have a virtual server that just redirects to the www address. The 301 response means that SEO vendors will know that this is a permanent move and that they should go to the www server. We have our DNS entries without the www pointed to the IP of the virtual server with this iRule:

     

     

    when HTTP_REQUEST {

     

    HTTP::respond 301 Location [http://www.[HTTP::host][HTTP::uri]]

     

    }
  • I'm rather confused, what IP address does the new VS use? Surely www.testsite.com and testsite.com resolve to the same IP address???
  • Melcaniac,,, i just tried that and it did not work this time i received a 504

     

     

    Steve - www.testsite.com is a different IP than testsite.com. I created a new entry for testsite.com because I could not modify the vs on the prod site at this time.
  • www.testsite.com points to vs1 with no irule just a pool

     

    testsite.com points to vs2 (different IP) trying make an irule that it points over to vs1 or inserts the www like melcaniac stated but that didnt seem to work
  • OK, you could try this very basic approach but it's not something I've ever done;

    
    when CLIENT_ACCEPTED {
     virtual other-virtual
    }
    

  • Better yet, use the same Pool on the new VS, assign a HTTP profile and use this;

    
    when HTTP_REQUEST {
     if { [string tolower [HTTP::Header "Host"] equals "testsite.com" } {
      HTTP::header replace Host "www.testsite.com"
      Stop processing the iRule for this event here
      return
      }
    }
    

    Just make sure there are no absolute links in server responses.
  • The iRule should be applied to the virtual server listening to the IP address for testsite.com (port 80). Is SSL involved or are we only dealing with HTTP reqeusts?
  • Another thing that would cause a 504 is a TCL error if you have other iRules applied to that virtual server and they are both trying to send a response or a redirect. Since it can't do both the F5 will not reply and will show a TCL error in /var/log/ltm. If that is not the only iRule applied to the virtual server then you may want to check the logs (and the logic). The iRule I posted earlier is exactly what we use in production, so it alone should give a 301. The other virtual server may have issues with the request, so I recommend using Fiddler to check the responses.
  • All thank you for all the help in this I have made the redirect work with the following irule, but you helped me get there so thanks.

     

    when HTTP_REQUEST {

     

    if { [HTTP::uri] eq "/" } {

     

    HTTP::redirect "http://www.testsite.com"

     

    }

     

    }

     

     

    Now since I have it redirecting correctly i'm going to try and make it work as a 301 instead of a 302
  • I just changed the irule to look like the following and it still works and gives me the 301 I was looking for

     

     

    when HTTP_REQUEST {

     

    set host [HTTP::host]

     

    set uri [HTTP::uri]

     

    HTTP::respond 301 Location "http://www.$host$uri"

     

    }

     

     

    Thanks again for everyone's help