Forum Discussion

Rustic_57941's avatar
Rustic_57941
Icon for Nimbostratus rankNimbostratus
Oct 17, 2013

Inserting "www" or redirecting to "www"

Hi,

 

My client has changed their back end servers from Coldfusion to Liferay. The Tomcat within the new Liferay only accepts "http://www.example.com" so when users try "http://example.com" it fails. So I wrote a basic iRule to redirect the users browser to use "www" in the request. Mostly it works fine, but the URI is lost and I'm really sure if this is the correct way to this.

 

Any suggestions?

 

Thanks!

 

iRule is:

 

when HTTP_REQUEST { set host [HTTP::host] if { [HTTP::host] equals "example.com" } { HTTP::respond 301 Location "http://www.$host/" } }

 

4 Replies

  • Was wondering if this would retain the URI: when HTTP_REQUEST { set host [HTTP::host] if { [HTTP::host] equals "example.com" } { HTTP::respond 301 Location "http://www.$host[HTTP::uri]" } }

     

  • You don't need to set a host variable. You can do something like that :

    when HTTP_REQUEST { 
        if {string tolower [HTTP::host] equals "example.com" } 
           { HTTP::respond 301 Location "http://www.[HTTP::host][HTTP::uri]" } 
           }
    

    This should work.

  • You will need to enclose the "string tolower" in brackets.

    when HTTP_REQUEST {
     if { [string tolower [HTTP::host]] equals "example.com" }{ 
         HTTP::respond 301 Location "http://www.[HTTP::host][HTTP::uri]"
     }
    }
    
  • Thanks Michael - your version with the brackets works prefect.

     

    Rgds, Mark