Thursday, July 23, 2015

How to redirect your website to its mobile version

Javascript window.location Method

Mobile phones typically have a small screen width. A simple trick is to redirect if the browser width less than or equal to 800 pixels. However when the mobile is horizontal the with can be increased 960 pixels or more. The tabs use larger with will still use the non mobile site.

Place this in the head section of your main website code.

<script type="text/javascript">
  if (screen.width <= 800) {
    window.location = "";
To get the best results, test it with various smart phones.

.htaccess URL rewrite redirects

Based on the MIME types browser support,  .htaccess can redirect visitors

RewriteEngine On
# Check for mime types commonly accepted by mobile devices
RewriteCond %{HTTP_ACCEPT} "text\/vnd\.wap\.wml|application\/vnd\.wap\.xhtml\+xml" [NC]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^{REQUEST_URI} [R,L]

This can check wap capability for wap apps which are no longer needed with smart phones

CSS3 media queries

Currently this is the best option as you can use different style sheets to render your web site on any device.

<!-- CSS media query on a link element -->

<link rel="stylesheet" media="(max-width: 800px)" href="device800.css" />

<!-- CSS media query within a stylesheet -->
@media (max-width: 600px) {
  .shilpa_sidebar {  
  display: none;

A good method is to seperate general and device specific css into seperate css scafolds.

/*General CSS here*/
font-family:Arial, "Sans-Serief";

@media (max-width: 1300px) {

@media (max-width: 540px) {

More advanced information on this

No comments:

Post a Comment