Make Your Site Mobile-Friendly in Two Minutes
January 7, 2010 | banay | Tutorial | No CommentsFour easy steps
Outlined below are the four steps to get this done in a matter of minutes, provided you are in an Apache environment and can run PHP. If you’re not, these steps can easily be adaptable to other technologies.
Step 1: Set up a domain mirror
If your site lives at www.myawesomeblog.com, you’re going to want to set up a subdomain at mobile.myawesomeblog.com. You want to set up your subdomain as a “mirror” of your main site, meaning the subdomain is really just pointing to your existing site.
Step 2: Create global_prepend file
The next thing we’re going to do is a create a PHP file which will be automatically prepended to every page of our site. Call this file something like "global_prepend.php" and throw it at the root of your server:
This code uses a PHP function called ob_start() to read in your entire HTML source, run some rules on it, and then send the output to users’ web browsers… all in real time. The first "if" statement simply checks to see if the user is coming from our special “mobile” URL, and if so, runs seven replace statements on the code. Here’s what each line does:
- Changes all URLs to “mobile”-ized URLs.
- Strips all linefeeds, carriage returns, and tabs.
- Trims multiple spaces down to one (HTML doesn’t recognize more than one space in a row).
- Changes any anchored images with alt text to plain text anchors.
- Strips all stylesheets, images, inline styles, scripts, and comments (including RDF).
- Tells search engine robots not to index or crawl the mobile version of the site so as to not create duplicate listings.
Step 3: Create global_append file
Next, we need to create a tiny PHP file which will automatically get added to the end of every file on our site. This is the code that actually outputs the page to the browser. So the flow is like so: Suck code into buffer, siphon fat away, spit contents of buffer into browser.
The code for the global_append file is below. Call it something like "global_append.php" and throw it at the root of your server:
Step 4: Enable prepends and appends using .htaccess
If you don’t already have an .htaccess file at the root of your server, open up a new text file and add these lines to it:
php_value auto_prepend_file /localfilepath/global_prepend.php
php_value auto_append_file /localfilepath/global_append.php
Then save it to the root of your server with the filename ".htaccess". If you already have an .htaccess file, just add the above lines to it.
* Important Note: If you copy these two lines from your web browser, you might need to delete the carriage return and make your own. Sometimes a browser’s carriage return will cause your .htaccess file to fail (you’ll know immediately if it has failed because your site won’t come up).
Assuming your subdomain is live, you should now be able to hit your site in a web browser using the special mobile URL and see a nice, compact, imageless, styleless, scriptless version of your site. Good Luck !



