Forum Discussion
Redirect domain apex to www, retain URL/protocol
Hi!
We're proud owners of some new Big-IP virtual licenses. Being new to Big-IP, our iRule experience is little to none, so I was hoping we could get some assistance in generating an iRule that would do the following:
Redirect website.com to , retaining protocol (HTTP or HTTPS) and the full URL, but NOT do this for subdomains.
For example: https://website.com/pages/subpage.html redirects to https://www.website.com/pages/subpage.html and http://website.com redirects to http://www.website.com but https://subdomain.website.com does NOT redirect
Does anybody have an iRule that does this, or is there a better way to do it in Big-IP LTM? I realize it might be specific with retaining the full path and protocol but excluding subdomains.
Thanks for any help you guys can provide!
- Jesse
Hi and welcome,
Recommend to take a look at ready-made iRules in Codeshare to get started. Start by making minor adjustments to someone else's code and learn that way. Basic iRule writing skills are a must when working with BigIP LTM.
This code will accomplish your current task
when CLIENT_ACCEPTED { Default to HTTPS set proto "https" if { [TCP::local_port] eq "80" }{ set proto "http" } } when HTTP_REQUEST { if { [string tolower [HTTP::host]] eq "website.com" }{ HTTP::respond 301 Location "$proto://www.website.com[HTTP::uri]" Connection Close event disable } }
Requires HTTP profile attached to your Virtual Server
Rgds,
- Hannes_Rapp_162Nacreous
Hi and welcome,
Recommend to take a look at ready-made iRules in Codeshare to get started. Start by making minor adjustments to someone else's code and learn that way. Basic iRule writing skills are a must when working with BigIP LTM.
This code will accomplish your current task
when CLIENT_ACCEPTED { Default to HTTPS set proto "https" if { [TCP::local_port] eq "80" }{ set proto "http" } } when HTTP_REQUEST { if { [string tolower [HTTP::host]] eq "website.com" }{ HTTP::respond 301 Location "$proto://www.website.com[HTTP::uri]" Connection Close event disable } }
Requires HTTP profile attached to your Virtual Server
Rgds,
- Jesse_Reinhart_Nimbostratus
Thanks so much! I was reading this when you replied:
 
https://devcentral.f5.com/s/articles/the101-irules-ndash-introduction-to-irules
 
However, I was unaware of codeshare. I'll definitely check that out!
 
- Jesse
- Hannes_RappNimbostratus
Hi and welcome,
Recommend to take a look at ready-made iRules in Codeshare to get started. Start by making minor adjustments to someone else's code and learn that way. Basic iRule writing skills are a must when working with BigIP LTM.
This code will accomplish your current task
when CLIENT_ACCEPTED { Default to HTTPS set proto "https" if { [TCP::local_port] eq "80" }{ set proto "http" } } when HTTP_REQUEST { if { [string tolower [HTTP::host]] eq "website.com" }{ HTTP::respond 301 Location "$proto://www.website.com[HTTP::uri]" Connection Close event disable } }
Requires HTTP profile attached to your Virtual Server
Rgds,
- Jesse_Reinhart_Nimbostratus
Thanks so much! I was reading this when you replied:
https://devcentral.f5.com/articles/the101-irules-ndash-introduction-to-irules
However, I was unaware of codeshare. I'll definitely check that out!
- Jesse
Recent Discussions
Related Content
* 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