Skip to content

Configuring a mobile subdomain for apache

peakpg edited this page Mar 28, 2012 · 3 revisions

This module relies heavily on Apache and mod_rewrite in order to automatically detect what device a user is browsing the site, and redirect them accordingly. These steps cover how to configure Apache so that will end up with the following subdomains.

  • www.example.com - Public 'full' site for visitors (Cached)
  • cms.example.com - Admin access (uncached)
  • m.example.com - Mobile subdomain (Cached)

Sample Production Environment

The first two (www and cms) are the standard CMS subdomains you configure as per the Deployment Guide. The new mobile subdomain will serve the mobile content. Anytime you see one of the above domain names below, just substitute your project's domain for it. Here is a sample conf file for it.

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot "/var/www/vhosts/examplesite/public"
   ServerName www.example.com
   RailsEnv production
   RewriteEngine On

   # Set a 'full site' cookie if the user wants to stay on the full site. (2 weeks cookie by default)
   RewriteCond %{QUERY_STRING} prefer_full_site=true
   RewriteRule ^/?(.*)$ http://www.example.com/$1? [L,CO=prefer_full_site:true:.example.com:840]

   # If the user wants the mobile site, set the cookie, redirect and stop.
   RewriteCond %{QUERY_STRING} prefer_mobile_site=true
   RewriteRule ^/?(.*)$ http://m.example.com/$1? [L,CO=prefer_full_site:false:.example.com:840]

   # Redirect Mobile users to m. based on user agent
   RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
   RewriteCond %{HTTP_COOKIE} !prefer_full_site=true
   RewriteRule ^/(.*) http://m.example.com/ [L,R=302]

   # Page Caching
   RewriteRule ^/$ /cache/index.html [QSA]
   RewriteRule ^([^.]+)$ /cache/$1.html [QSA]

   # Rewrite Logging (Can be commented out once you verify it works)
   RewriteLog /var/sites/mysite/log/rewrite.log
   RewriteLogLevel 9
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot "/var/www/vhosts/examplesite/public"
   ServerName m.example.com
   RewriteEngine On
   RewriteRule ^/$ /mobile_cache/index.html [QSA]
   RewriteRule ^([^.]+)$ /mobile_cache/$1.html [QSA]
   RailsEnv production
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot "/var/www/vhosts/examplesite/public"
   ServerName cms.example.com
   RailsEnv production
</VirtualHost>

Sample Development Environment

Here is a sample apache.conf file for testing browsercms locally. The only thing that should need to be changed is the path to where the project root is, i.e.

/Users/username/projects/your_site/public

NameVirtualHost *:80

<VirtualHost *:80>
   DocumentRoot "/Users/username/projects/your_site/public"
   ServerName www.mobile.local

   RailsEnv production
   RewriteEngine On

   # Set a 'full site' cookie if the user wants to stay on the full site. (2 weeks cookie by default)
   RewriteCond %{QUERY_STRING} prefer_full_site=true
   RewriteRule ^/?(.*)$ http://www.mobile.local/$1? [L,CO=prefer_full_site:true:.mobile.local:840]

   # If the user wants the mobile site, set the cookie, redirect and stop.
   RewriteCond %{QUERY_STRING} prefer_mobile_site=true
   RewriteRule ^/?(.*)$ http://m.mobile.local/$1? [L,CO=prefer_full_site:false:.mobile.local:840]

   # Redirect Mobile users to m. based on user agent
   RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
   RewriteCond %{HTTP_COOKIE} !prefer_full_site=true
   RewriteRule ^/(.*) http://m.mobile.local/ [L,R=302]

   # Page Caching
   RewriteRule ^/$ /cache/index.html [QSA]
   RewriteRule ^([^.]+)$ /cache/$1.html [QSA]

   # Cache Logging (Not strictly necessary)
   RewriteLog /Users/username/projects/your_site/log/rewrite.log
   RewriteLogLevel 9
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot "/Users/username/projects/your_site/public"
   ServerName m.mobile.local

   RewriteEngine On
   RewriteRule ^/$ /mobile_cache/index.html [QSA]
   RewriteRule ^([^.]+)$ /mobile_cache/$1.html [QSA]

  RailsEnv production
   
</VirtualHost>

<VirtualHost *:80>
   DocumentRoot "/Users/username/projects/your_site/public"
   ServerName cms.mobile.local
   RailsEnv production   
</VirtualHost>
Clone this wiki locally