Depending on how your website is deployed and managed, you can setup the re-direct at either the main web server entry point (e.g. Apache, .NET), or your CDN (Content Distribution Network) provider. Scroll down for sample code to setup a redirect for a) Smartphone & Tablet b) Tablet only.
- Locate the mobile URL for the property’s newly created mobile website(s).
(This should be something like http://m.myhotel.com) - Select the appropriate script below and plug in your mobile URL. Replace the sample URL (http://m.myhotel.com) in the code below, with the URL for your property's mobile website.
SMARTPHONE & TABLET
1. Apache Web Server
This example shows how you would switch the links at the Apache server level in the .htaccess configuration file using mod_rewrite.
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk" [NC]
RewriteCond %{QUERY_STRING} !^fullsite=true [NC]
RewriteRule ^(.*)$ http://m.myhotel.com [L,R=302]
2. JavaScript
The following snippet shows how your page can easily be redirected on the client browser:
var ua = navigator.userAgent; if ((ua.match(/touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk/i)) && !window.location.href.match(/fullsite=true/i)){ location.replace("http://m.myhotel.com");
}
To prevent the your normal website page from fully loading prior to redirecting to the Smartphone or Tablet Website, ensure that the JavaScript above is placed ahead of other JavaScript code that you may have on your page which is dedicated to rendering content.
3. Java- J2EE Example
This would hook in to the doPost or doGet methods:
String ua = request.getHeader("user-agent").toLowerCase();
if(request.getQueryString().indexOf("fullsite=true") < 0){
if (ua.matches("(?i).*(touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk).*")){
response.sendRedirect("http://m.myhotel.com");
}
}
4. PHP Server side example:
$fullsite = "false"; if(isset($_GET['fullsite'])){ $fullsite = $_GET['fullsite']; } if(strcasecmp($fullsite, 'true') !==0){ $ua = $_SERVER['HTTP_USER_AGENT']; if (preg_match("/touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk/i", $ua)){ header('http://m.myhotel.com');
exit;
} }
5. ASP.NET
The following C# example shows how redirection can be achieved by including following code into the Page_Load handler.
string ua = Request.UserAgent.ToString().ToLower(); if (ua != null){ if (Request.Url.indexOf("fullsite==false") < 0 && Request.Browser.IsMobileDevice == true){
if (Regex.Match(ua, @"(?i).*(touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk).*").Success){ Response.Redirect("http://m.myhotel.com"); } } }
TABLET ONLY
1. Apache Web Server
This example shows how you would switch the links at the Apache server level in the .htaccess configuration file using mod_rewrite.
RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk" [NC]
RewriteCond %{QUERY_STRING} !^fullsite=true [NC]
RewriteRule ^(.*)$ http://m.myhotel.com [L,R=302]
2. JavaScript
The following snippet shows how your page can easily be redirected on the client browser:
var ua = navigator.userAgent; if ((ua.match(/touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk/i)) && !window.location.href.match(/fullsite=true/i)){ location.replace("http://m.myhotel.com");
}
To prevent the your normal website page from fully loading prior to redirecting to the Smartphone or Tablet Website, ensure that the JavaScript above is placed ahead of other JavaScript code that you may have on your page which is dedicated to rendering content.
3. Java- J2EE Example
This would hook in to the doPost or doGet methods:
String ua = request.getHeader("user-agent").toLowerCase();
if(request.getQueryString().indexOf("fullsite=true") < 0){
if (ua.matches("(?i).*(touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk).*")){
response.sendRedirect("http://m.myhotel.com");
}
}
4. PHP Server side example:
$fullsite = "false";
if(isset($_GET['fullsite'])){
$fullsite = $_GET['fullsite'];
}
if(strcasecmp($fullsite, 'true') !==0){
$ua = $_SERVER['HTTP_USER_AGENT'];
if (preg_match("/touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk/i", $ua)){
header('http://m.myhotel.com');
exit;
}
}
5. ASP.NET
The following C# example shows how redirection can be achieved by including following code into the Page_Load handler.
string ua = Request.UserAgent.ToString().ToLower();
if (ua != null){
if (Request.Url.indexOf("fullsite==false") < 0 && Request.Browser.IsMobileDevice == true){
if (Regex.Match(ua, @"(?i).*(touch|tablet|mobile|android|ipad|blackberry|rim|phone|silk).*").Success){
Response.Redirect("http://m.myhotel.com");
}
}
}
Comments
0 comments
Please sign in to leave a comment.