Forum Discussion
Mobile iRule help to stay on MainSite
Hello all,
I have an iRule which redirects me to mobile website. This mobile website sends in Request Parameter STOP_MOBI=Yes (Stop Mobile Website and render main website) to stay on the Main Website.
Till here, I am able to work with redirection properly. But, once it goes to main site it loops back in to Mobile site. To avoid this situation, I have written something to Write a cookie through HTTP_RESPONSE, but while doing so, I am having following issue. If I can write this cookie to response, I can avoid this Loop Back issue.
When it executes HTTP_RESPONSE, LTM log shows following error
HTTP_RESPONSE> - can't read "stop_mobi_cookie": no such variable while executing "log local0. $stop_mobi_cookie"
This is the code snippet of my iRule
6 Replies
- Kevin_Stewart
Employee
Some quick thoughts:
-
If you set a variable in the HTTP_REQUEST event, you should look for in the HTTP_RESPONSE event with an [info exists ] command. Example:
when HTTP_RESPONSE { if { [info exists stop_mobi_cookie] } { ... } }
-
Using the above logic, you should only need to set/create the variable in the "yes" branch of the URI::query evaluation (to a value of 1). If you need the ability to switch back and forth after the user has the cookie, you can optionally add in the zero value as you've done, and then evaluate that in HTTP_RESPONSE.
-
You shouldn't need the 'set stop_mobi_cookie 1` expression in the "1" clause of the cookie switch expression, because the cookie already equals 1. At best you probably just need a **return** statement.
-
If you don't need to be able to switch back and forth after the user has the stop_mobi cookie, then your iRule can be a lot simpler - just checking for the existence of the cookie or the existence and value of the query string.
-
- HD_117145
Nimbostratus
Thank Kevin. It really gave me a good input.
No. I need not to do back and forth. Once user selects to stay on Mobile site. It need not to comeback to mobile.
As you mentioned in point 4. Can you please help me with a some psudo code to make it simple?
- Kevin_Stewart
Employee
Try this:
when HTTP_REQUEST { if { [string tolower [HTTP::host]] contains "www.mysite.com" } { switch -glob [string tolower [HTTP::header User-Agent]] { "*androind*mobile*" - "*iphone*" - "*ipod*" { if { [HTTP::cookie exists stop_mobi] } { cookie exists - do nothing (stay here) return } else { switch [URI::query [HTTP::uri] stop_mobi] { "yes" { URI exists and equals "yes" set stop_mobi_cookie 1 } default { URI doesn't exist - redirect HTTP::redirect "https://m.mysite.com" TCP::close event disable } } } } } } } when HTTP_RESPONSE { if { [info exists stop_mobi_cookie] } { unset stop_mobi_cookie HTTP::cookie insert name stop_mobi value 1 } }
- HD_117145
Nimbostratus
Thanks Kevin.
I have one small adjustment to make for one additional requirement, I am not sure of the optimal solution though.
Once this Cookie is created, it should expire after 30 minutes if no activity is performed on the website.
- Kevin_Stewart
Employee
Take a look at the HTTP::cookie expires command wiki page:
https://devcentral.f5.com/wiki/iRules.HTTP__cookie.ashx
Essentially you'd add this command directly after the HTTP::cookie insert command. Without setting an expires tag on a cookie, the cookie will be "session-based", or rather in browser memory only (flushed when the browser is closed). With an expires tag the cookie becomes file-based - a physical file in the browsers cookie cache.
- HD_117145
Nimbostratus
Thanks Kevin. Your expertise and contribution was very helpful. :)
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