Trouble with redirect

I’m trying to write a redirect from the root level of a domain(ex domain.com), to www.domain.com. I know the best and easiest way would be to just put a redirect on the web server, but as sane as that is, its not an option for me.

So here is my iRule.

when HTTP_REQUEST{

if { [HTTP::path] equals “http://domain.com”}

{HTTP::redirect “http://www.domain.com”}

}

I’m getting some generic errors from the iRule editor that aren’t of much help. Any assistance would much appreciated. Also if it helps I am running 11.3.

Should it not be [HTTP::host] instead of [HTTP::path]?

You are right, I’ve updated the iRule with host and it still seems unhappy about something in the redirect. when HTTP_REQUEST { if { [HTTP::host] equals “http://domain.com” } { HTTP::redirect “http://www.domain.com” } }

Notice the error with “http://” string in your host equals statement? Protocol is not part of the HTTP host header :slightly_smiling_face:

when HTTP_REQUEST {
  if { [HTTP::host] equals "domain.com" }
  { HTTP::redirect "http://www.domain.com" }
}

A more universal solution for the missing www. problem is below (a single iRule can be used repeatedly for multiple applications):

when HTTP_REQUEST {
  if { not([HTTP::host] starts_with "www.") } {
    HTTP::respond 302 location "http://www.[HTTP::host][HTTP::uri]"
    event disable
    TCP::close
  }
}