stephenjbell

Redirect between mobile and full domains - dotCMS

Aug 31st, 2011
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.02 KB | None | 0 0
  1. ## Intelligently send people back and forth between different versions of
  2. ## the same page on mobile and full domains depending on
  3. ## 1) their devices, 2) whether or not the URL has "?siteversion=full" or ?siteversion=mobile
  4. ## and 3) whether cookies have been set by the other two conditions.
  5. ##
  6. ## Logic flowchart (rough) - http://twitpic.com/6e4woa
  7. ##
  8. ## This code makes use of ISAAC's mobile detection plugin:
  9. ## http://geekyplugins.com/dot-cms/mobile-visitor-detection.dot
  10.  
  11.  
  12.  
  13. ## Your site domains
  14. #set($fullDomain = "www6.oc.edu")
  15. #set($mobileDomain = "m.oc.edu")
  16.  
  17. ###################### SET VARIABLES DEPENDENT ON CURRENT PAGE ADDRESS
  18.  
  19. ## Domain
  20. #set($requestURL = "$request.getRequestURL()")
  21. ## Detect if you're on the mobile domain by analyzing the site URL (does it contain my mobile domain?)
  22. #set($onMobileDomain = $requestURL.matches("^https?:\/\/($mobileDomain).*"))
  23.  
  24. #if($onMobileDomain == true)
  25. #set($otherVersion = "full")
  26. #set($currentVersion = "mobile")
  27. #set($otherDomain = $fullDomain)
  28. #else
  29. #set($otherVersion = "mobile")
  30. #set($currentVersion = "full")
  31. #set($otherDomain = $mobileDomain)
  32. #end
  33.  
  34. ## Page URL
  35. ## Some kung fu to find the page address if we're in URL mapped content
  36. #if($URLMapContent)
  37. #set($currentURLMapContent = $dotcontent.find($URLMapContent.identifier))
  38. #set($pageUri = $currentURLMapContent.getUrlMap())
  39. #else
  40. #set($pageUri = $request.getRequestURI())
  41. #end
  42.  
  43. ## QUERY STRING
  44. #set($queryString = $request.getQueryString())
  45.  
  46. ## Remove "siteversion" variable from query string before passing to another site (to prevent repetition)
  47. #set($queryString = $queryString.replaceAll("(siteversion=[a-zA-Z]*&?)|(&?siteversion=[a-zA-Z]*)$",""))
  48. #if($UtilMethods.isSet($queryString))
  49. #set($queryString = "?$queryString") ## Add the question mark
  50. #end
  51.  
  52. ## If there's already a query string, add siteversion onto it, otherwise make it just siteversion
  53. #if($UtilMethods.isSet($queryString))
  54. #set($siteversionquery = "&siteversion=${otherVersion}")
  55. #else
  56. #set($siteversionquery = "?siteversion=${otherVersion}")
  57. #end
  58.  
  59.  
  60. #set($otherUrl = "http://${otherDomain}${pageUri}$!{queryString}") ## The mirror of this page on the other server
  61. #set($otherUrlwSiteversion = "$otherUrl$!{siteversionquery}") ## The mirror of this page, with siteversion=??? to set a cookie there
  62.  
  63.  
  64.  
  65. ###################### MACROS
  66.  
  67. #macro(redirectme $url)
  68. ## Keep browsers from remembering this redirect in case people change their mind on the site version
  69. $response.setHeader("Pragma", "no-cache")
  70. $response.setHeader("Cache-Control","no-store")
  71.  
  72. ## Send me somewhere
  73. $response.sendRedirect($url)
  74. #end
  75.  
  76. #macro(siteversioncookie $cookievalue)
  77. ## Get a date 30 days from now to feed to cookie
  78. #set($now = $date.format('MM/dd/yyyy HH:mm:ss', $date.getDate()))
  79. #set($now30= $date.getDate().getTime() + 2592000000) ## Add 30 days
  80. #set($now30 = $date.format('E, dd-MMM-yyyy HH:mm:ss zzz', $now30))
  81.  
  82. ## Set the cookie
  83. $response.setHeader("Set-Cookie","siteversion=$cookievalue;expires=$!{now30};path=/;domain=oc.edu")
  84. #end
  85.  
  86. ## Example usage
  87. ## #redirectme("http://www.garfield.com")
  88. ## #siteversioncookie("mobile")
  89. ## $cookietool.siteversion.value
  90.  
  91.  
  92. ###################### START REDIRECTING
  93.  
  94.  
  95. ## Based on QUERY STRING
  96.  
  97. #if($UtilMethods.isSet($request.getParameter("siteversion")))
  98.  
  99. #if($request.getParameter("siteversion")==$otherVersion)
  100. #siteversioncookie($otherVersion)
  101. #redirectme($otherUrl)
  102. #elseif($request.getParameter("siteversion")==$currentVersion)
  103. #siteversioncookie($currentVersion)
  104. #end
  105.  
  106. #end
  107.  
  108.  
  109. ## Based on COOKIE
  110.  
  111. #if($cookietool.siteversion.value==$otherVersion && $UtilMethods.isSet($request.getParameter("siteversion")) != true)
  112. #redirectme($otherUrl)
  113. #end
  114.  
  115.  
  116. ## Based on device detection
  117.  
  118. ## Only detect if we're not doing something fancy with cookies or query strings
  119. #if($UtilMethods.isSet($cookietool.siteversion.value) != true && $UtilMethods.isSet($request.getParameter("siteversion")) != true)
  120. ## If it's a mobile device that isn't a tablet, serve mobile
  121. #if($mobileSite.device.getCapability('is_wireless_device')==true && $mobileSite.device.getCapability('is_tablet')==false)
  122. #siteversioncookie("mobile")
  123. #if($currentVersion != "mobile")
  124. #redirectme($otherUrlwSiteversion)
  125. #end
  126. #else
  127. #siteversioncookie("full")
  128. #if($currentVersion != "full")
  129. #redirectme($otherUrlwSiteversion)
  130. #end
  131. #end
  132.  
  133. #end
  134.  
  135. ###################### SET VARIABLE THAT CHANGES TEMPLATE
  136.  
  137. ## If we're on the mobile domain, show the mobile version of the site
  138. #set($useMobileVersion = $onMobileDomain)
  139.  
  140.  
  141. ###################### EXAMPLE USAGE IN PAGE
  142.  
  143.  
  144. #*
  145. #if($useMobileVersion)
  146.  
  147. Mobile version template code here
  148. <a href="$otherUrlwSiteversion">Full Site</a>
  149.  
  150. #else
  151.  
  152. Full version template code here
  153. <a href="$otherUrlwSiteversion">Mobile Site</a>
  154.  
  155. #end
  156. *#
Advertisement
Add Comment
Please, Sign In to add comment