Alternate and Other Automatic Redirection Scripts

Overview

On this page, you will find many of the different code's that Duda Mobile provides for redirecting your site. By default Duda provides the JavaScript redirect code to embed directly into your HTML, but we also offer other server side options to assist in getting the redirect setup as well.

JavaScript Redirects

Standard redirect

This is the redirect you will use for 90% of your sites. If you are a direct DudaMobile user, install this redirect in your header:

<script type="text/javascript" src="http://static.mobilewebsiteserver.com/redirect.js"></script>
<script type="text/javascript">DM_redirect("ENTER_MOBILE_URL_HERE");</script>

Client Side Redirect using JavaScript

This method is the easiest to install. Simply paste the javascript code below into the head tag of your HTML page, and replace in the code the " http://m.example.com " with your mobile website domain (including http://).

<script src="http://static.dudamobile.com/DM_redirect.js" type="text/javascript"></script>
<script type="text/javascript">
   DM_redirect("http://m.example.com");
</script>

Note: some mobile devices like old blackberry models don't support JavaScript, if you would like to support those devices as well, you will need to use server-side redirection

Note: You will need to set up this redirect on all pages which you want to make mobile friendly.

HTTPS redirect

If you have a site that's using HTTPS or SSL, the standard redirect script won't work when loaded. Instead, use our HTTPS compatible redirect script.

Redirect script for direct users:

<script type="text/javascript" src="https://s3.amazonaws.com/static.dudamobile.com/DM_redirect.js"></script>
<script type="text/javascript">DM_redirect("ENTER_MOBILE_URL_HERE");</script>

Cookie redirect

If your mobile site requires that users visit the desktop site on their mobile phones, you may encounter the issue with the redirect sending users in a loop due to the standard redirect script. To fix this, you can use cookies to tell the browser to not redirect after the user clicks a link to visit the desktop site (usually located in the footer of your website).

<script type="text/javascript" src="http://static.dudamobile.com/DM_redirect_cookie.js"></script>
<script type="text/javascript">DM_redirect("ENTER_MOBILE_URL_HERE");</script>

Note, that once a user visits the desktop website on their phones, the cookie redirect will stop redirecting users to the mobile site unless they clear their cookies and cache. Only use the cookie redirect if visiting the desktop site is a required.

Cookie and HTTPS redirect

A combination of the HTTPs and Cookie redirect. Remember to use the cookie redirect if required.

<script type="text/javascript" src="http://s3.amazonaws.com/DM_redirect_cookie.js"></script>
<script type="text/javascript">DM_redirect("ENTER_MOBILE_URL_HERE");</script>

True redirect script

You can use this redirect script to redirect users directly to a page on your mobile site. This is useful if you're experiencing certain issues with redirecting to the wrong homepage.

Note, that once a user visits the desktop website on their phones, the cookie redirect will stop redirecting users to the mobile site unless they clear their cookies and cache. Only use the cookie redirect if visiting the desktop site is a required.

<script src="http://static.dudamobile.com/DM_redirect.js" type="text/javascript"></script>
<script type="text/javascript"> DM_redirect("ENTER_MOBILE_URL_HERE", true);</script>

Redirect using server side snippet

The server-side method might be more complex to implement but will ensure that mobile devices who do not support javascript are supported as well. We offer code examples for PHP, ASP and JSP. Please replace the placeholder " http://m.example.com" in the code with your mobile website domain (including http://).

PHP

Paste the code below in the very beginning of your index.php file. Be sure to edit the m.example.com with your own mobile URL.

<?php
$mobileDomain = "http://m.example.com";
$no_redirect = @$_REQUEST['no_redirect'];
if($no_redirect != "true")
{
    $agent = @$_SERVER['HTTP_USER_AGENT'];
    @ini_set('default_socket_timeout',1);
    $handle = @fopen("http://mobile.dudamobile.com/api/public/detect?ua=" . urlencode($agent), "r");
    @stream_set_timeout($handle, 1);

    /* check if we should redirect*/
    $result = @fread ( $handle , 1 );

    @fclose($handle);
    if ($result == "y") {
        $currenturl = "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];
        $mobileUrl = $mobileDomain ."?url=" .urlencode($currenturl);
        $mobileUrl=$mobileUrl."&dm_redirected=true";
        header("Location: ".$mobileUrl);
        exit;
    }
}
?>

ASP

Paste the code below in the very beginning of your index.asp file. Be sure to edit the m.example.com with your own mobile URL.

<%Dim no_redirect, detecturl, handle, output, currenturl, mobileUrl
no_redirect = Request.QueryString("no_redirect")
If no_redirect <> "true" Then
currenturl = ( "http://" & Request.ServerVariables("SERVER_NAME") & Request.ServerVariables("URL") ) & "?" & Request.ServerVariables("QUERY_STRING")
detecturl = "http://mobile.dudamobile.com/api/public/detect?ua=" & Server.UrlEncode(Request.ServerVariables("HTTP_USER_AGENT"))
set handle = CreateObject("MSXML2.ServerXMLHTTP")
handle.setTimeouts 5000,5000,5000,5000
handle.open "GET", detecturl, false
handle.send ""
output = handle.responseText
mobileUrl = "http://m.example.com" & "?url=" & Server.UrlEncode(currenturl) & "&dm_redirected=true"
If output = "y" Then
Response.Redirect(mobileUrl)
End If
End If
%>

JSP

Paste the code below in the very beginning of your index.jsp file. Be sure to edit the m.example.com with your own mobile URL.

<%@page import="java.net.URLEncoder"%>
<%@page import="java.net.URL"%>
<%@page import="java.net.HttpURLConnection"%>
<%
if(! "true".equals(request.getParameter("no_redirect")))
{
String currenturl = request.getRequestURL() + (request.getQueryString() != null ? "?" + request.getQueryString() : "");
try{
String ua = URLEncoder.encode(request.getHeader("user-agent"), "utf-8");
HttpURLConnection con = (HttpURLConnection)new URL("http://mobile.dudamobile.com/api/public/detect?ua=" + ua).openConnection();
con.setConnectTimeout(5000); //set timeout to 5 seconds
char c = (char)con.getInputStream().read();
con.disconnect();
if(c == 'y')
{
String mobileUrl = "http://m.example.com";
response.sendRedirect(mobileUrl + "?url=" + URLEncoder.encode(currenturl) + "&dm_redirected=true");
}
}
catch(Exception e){
e.printStackTrace();
}
}
%>

.htaccess (Advanced)

Install the following code at the bottom of your existing .htaccess file. Please be sure to backup your existing .htaccess file before you make changes. Installing the .htaccess code incorrectly can bring down your website easily.

Make sure you replace any reference of example.com with your own domain.

RewriteEngine on
######### Set cookie for users who return to desktop site
RewriteBase /
RewriteCond %{QUERY_STRING} no_redirect=true [NC]
RewriteRule ^(.*)$ - [co=dm_show_classic:true:.example.com:7200:/]
# check no 'dm_show_classic' cookie is set
RewriteCond %{HTTP_COOKIE} !dm_show_classic
# check if no_redirect was sent in the url
RewriteCond %{QUERY_STRING} !no_redirect=true [NC]
# check if the user agent matches supported mobile devices
RewriteCond %{HTTP_USER_AGENT} ((.*iPhone.*)|(.*iPod.*)|(.*BlackBerry.*)|(.*Android.*)|(.*webOS.*)|(.*Windows\ CE.*)|(.*IEMobile.*)|(.*Opera\ Mini.*)|(.*Opera\ Mobi.*)|(.*HTC.*)|(.*LG-.*)|(.*LGE.*)|(.*SAMSUNG.*)|(.*Samsung.*)|(.*Symbian.*)|(.*Nokia.*)|(.*PlayStation.*)|(.*PLAYSTATION.*)|(.*Nintendo\ DSi.*)|(.*TMO-US_LEO.*)|(.*SEC-SGH.*)|(.*BB10.*))
# check if the request isn't for a static file
RewriteCond %{REQUEST_FILENAME} !\.(jpg|gif|png|css|js|txt|ico|pdf|bmp|tif|mp3|wav|wma|asf|mp4|flv|mpg|avi|csv|doc|docx|xls|xlsx|ppt|pptx|zip|rar|tar|gz|dmg|iso)$ [NC]
#### If query string is not empty, then include ##############
RewriteCond %{QUERY_STRING} !^$
# extract the current page url including the query string
RewriteCond http://%{HTTP_HOST}%{REQUEST_URI}?%{QUERY_STRING} ^(.*)$
# redirect the request to mobile domain and pass the page url as a parameter
RewriteRule ^(.*)$ http://m.example.com?url=%1 [R=302,L]
×

Priority Phone Support

English phone support is available 18 hours a day,
Monday through Friday, 3am to 9pm (EST).

United States +1 866-776-1550 3am to 9pm (Eastern)
United Kingdom +44 (0)800-011-9071 8am to 2am (London)
France +33 (0)9-75-18-84-74 9am to 3am (Paris)
Israel +972 (0)3-720-8922 10am to 4am (Jerusalem)
Australia +61 (0)2-8880-9166 7pm to 1pm (Sydney)
To schedule a call in French, Portuguese, Hebrew or Russian, please email us at prioritysupport@dudamobile.com
×

Please Log in as a DudaPro

Priority Phone Support is available exclusively to DudaPros. Log in to your DudaPro account now to see our international support numbers.

Log In
Not a DudaPro? Start a free trial now!