Forum Discussion

Ryan_McDonald_8's avatar
Ryan_McDonald_8
Icon for Nimbostratus rankNimbostratus
Aug 07, 2009

Redirect to www

If a customer types in domainname.com I want them to be redirected to www.domainname.com.

 

 

I used the irule generator and came up with this

 

 

when HTTP_REQUEST {

 

if {[HTTP::uri] equals {http://scadalynx.com}} {HTTP::uri {http://www.scadalynx.com}

 

}

 

}

 

 

It doesn't seem to be working though. I have it applied to my port 80 virtual server. I also have a rule that then requires SSL and redirects them to https. The www rule is first. Ideally I would like one rule to do both, but any help making my www rule work is greatly appreciated.

 

 

TIA,

 

 

-Ryan

4 Replies

  • A couple of points.

    First, The URL is in the form of protocol://[HTTP::host][HTTP::uri]. In your case, if you are searching for a domain, then you can just reference the [HTTP::host] variable.

    For hostname modifications there are two ways to go about it. First, you can issue a HTTP redirect to the browser with the HTTP::redirect command. This will send a response back to the browser with the new address and the browsers address bar will be updated with the new address. Second, you could transparently change the host name my modifying the Host header with the HTTP::header command. This will not send an update to the browser but only change what the backend server "thinks" was typed in the browsers address bar.

    It seems that you want to issue a full redirect to the client so this example should do the trick.

     when HTTP_REQUEST { 
       if { [string tolower [HTTP::host]] eq "scadalynx.com" } { 
         HTTP::redirect "http://www.[HTTP::host][HTTP::uri]" 
       } 
     }

    This will keep the existing hostname but prepend a www to it. So a request from http://foo.com/index.html will be redirected to http://www.foo.com/index.html.

    If you need this for your SSL virtual as well, it will be easiest to have two iRules with different redirect statements. If you really want one, you can get the protocol from the TCP::local_port. If that equals 443 then you can use "https" instead of "http" in the redirect. Something like this should work.

     when HTTP_REQUEST { 
       if { [string tolower [HTTP::host]] eq "scadalynx.com" } { 
         if { [TCP::local_port] == 443 } { 
           HTTP::redirect "https://www.[HTTP::host][HTTP::uri]" 
         } else { 
           HTTP::redirect "http://www.[HTTP::host][HTTP::uri]" 
         } 
       } 
     }

    Hope this helps...

    -Joe
  • Hi Joe,

     

     

    I copied the code from the first rule you provided. Firefox works just fine. IE doesn't. IE acts like it is disconnected from the internet. I have it running on a test domain if you wanted to see. http://teamelynx.com. Any ideas?

     

     

    Thanks,

     

     

    -Ryan
  • Couple of other pieces of information.

     

     

    If I take the require ssl rule out it works just fine. SSL iRule = when HTTP_REQUEST { HTTP::redirect "https://[HTTP::host][HTTP::uri]"}

     

     

    I saw this in the log:

     

     

    TCL error: ssl_require - Operation not supported. Multiple redirect/respond invocations not allowed (line 1) invoked from within "HTTP::redirect "https://[HTTP::host][HTTP::uri]""
  • I was able to get the second rule to work with one minor change. (It wasn't redirecting http://www.scadalynx.com to https://www.scadalynx.com)

     

     

     

    when HTTP_REQUEST {

     

    if {[string tolower [HTTP::host]] eq "domainname.com" } {

     

    if {[TCP::local_port] == 443 } {

     

    HTTP::redirect "https://www.[HTTP::host][HTTP::uri]"

     

    } else { HTTP::redirect "http://www.[HTTP::host][HTTP::uri]"

     

    }

     

    } else { HTTP::redirect "https://[HTTP::host][HTTP::uri]" }

     

    }

     

     

     

     

    So to summarize, this will take all http traffic to http://www.domainname.com or http://domainname.com and redirect it to https://www.domainname.com. It is applied on my port 80 virtual server.