Forum Discussion
Modify HTTP::Host and HTTP::URI in an iRule
I have a need to redirect requests from a subfolder, http://www.website.com/folder/a/b/ for example, to a subdomain of http://folder.website.com/a/b/. I am somewhat able to accomplish this but it doesn't solve for someone going to http://www.website.com/folder, as it takes the user to http://folder.website.com/folder which will not work.
when HTTP_REQUEST {
if { [HTTP::uri] starts_with "/folder"} {
set path [HTTP::uri]
set locate [string first "/" $path 1]
set cleanpath [string range $path $locate end]
HTTP::redirect "http://folder.website.com$cleanpath"
}
}
- P_KAltostratus
Is your uri's /folder/a/b/ and /a/b are static? can you explain what exactly you need? I may help out with an irule.
- THawesBPC_28376Nimbostratus
Thanks PK.
Sorry, I should have elaborated. folders a & b are not static and will be different depending on the content selected by the user, so those will need to be extracted from the URI and carried over as part of the redirect, removing only the first directory from it (/folder/). Does that make sense?
- P_KAltostratus
Thanks for explaining!
- THawesBPC_28376Nimbostratus
No problem!
Close, the first part is correct. However if a user hits http://www.website.com/folder, then they should just be redirected to http://folder.website.com
The /a/b/ might be /a/c/, or /d/f/, or any other combination.
- DevBabuCirrus
A little modification in the irule should help
when HTTP_REQUEST { if { [HTTP::uri] starts_with "/folder" && !([HTTP::uri] ends_with "/folder/")} { set path [HTTP::uri] set locate [string first "/" $path 1] set cleanpath [string range $path $locate end] HTTP::redirect "http://folder.website.com$cleanpath" log local0. "http://folder.website.com$cleanpath" } elseif { [HTTP::uri] starts_with "/folder" && [HTTP::uri] ends_with "/folder/"} { HTTP::redirect "http://folder.website.com" } }
- P_KAltostratus
Not sure but give this a try!
when HTTP_REQUEST {
- THawesBPC_28376Nimbostratus
Thanks PK & DevBabu! You were both able to lead me to the solution. I just needed to add one additional piece to solve for website.com/folder instead of website.com/folder/
when HTTP_REQUEST { if { [HTTP::uri] ends_with "/folder"} { HTTP::redirect "http://folder.website.com" } elseif { [HTTP::uri] starts_with "/folder" && !([HTTP::uri] ends_with "/folder/")} { set path [HTTP::uri] set locate [string first "/" $path 1] set cleanpath [string range $path $locate end] HTTP::redirect "http://folder.website.com$cleanpath" } elseif { [HTTP::uri] starts_with "/folder" && [HTTP::uri] ends_with "/folder/"} { HTTP::redirect "http://folder.website.com" } }
- P_KAltostratus
What do you mean? Do you not want to use trailing slash in the uri match??
- THawesBPC_28376Nimbostratus
In that case, this would happen:
http://www.website.com/folder would be redirected to http://folder.website.com/folder, which does not exist.
- P_KAltostratus
Yes True! But we can change that. I didn't get what's the issue now?
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