Forum Discussion
javier_diaz_379
Nimbostratus
May 04, 2011Rewrite URL
I want to accomplish the following:
Currently the web page we're serving both www. and non www.
I need a permanent redirect, so when a user requests the non www. version of the webpage it redirect to www.version
Thanks
3 Replies
- hoolio
Cirrostratus
Hi Javier,
Here is an iRule you can use to send a 301 redirect to the same host and URI with www. prepended:when HTTP_REQUEST { Check if Host header value has at least one alpha character and doesn't already start with www. if {[string match {[a-zA-Z]} [HTTP::host]] and not ([string tolower [HTTP::host]] starts_with "www."}{ Send a 301 redirect to the same host and URI with www. prepended HTTP::respond 301 Location "http://www.[HTTP::host][HTTP::uri]" } }
If you are using this on an HTTPS VS and you don't have a cert valid for both example.com and www.example.com, the client will get a mismatched cert warning before the iRule triggers.
Aaron - Michael_Yates
Nimbostratus
You didn't specify the Protocol, so here is an example of an iRule that you could use on an HTTP Virtual Server to redirect to HTTPS and add a WWW if needed:when HTTP_REQUEST { if {[HTTP::host] starts_with "www."} { HTTP::redirect "https://[HTTP::host][HTTP::uri]" } else { HTTP::redirect "https://www.[HTTP::host][HTTP::uri]" } } - Colin_Walker_12Historic F5 AccountHoolio wins the race again... ;)
Both fine examples.
Colin
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
DevCentral Quicklinks
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
Discover DevCentral Connects