linuxapachemobile

Problems with configuring Apache for mobile web site


I am having some trouble with configuring Apache for a web site that has both a normal and mobile version. The idea was to redirect the user with a mobile browser automatically to the mobile version and if someone on a normal desktop PC is trying to connect to the mobile version to redirect it to the normal web site.

Right now it redirects a mobile user to the mobile web site correctly, but it is seems to be unable to redirect the other way to the desktop version. Also, when you go to the mobile version of the web site, it displays the default "It works!" page instead of the mobile index page for some reason..

Here is all the configuration that I used. Hopefully someone is able to help me out with this.

Normal website:

<VirtualHost *:80>
ServerAdmin webmaster@henal.local
ServerName henal.local
ServerAlias www.henal.local

DocumentRoot /var/www/henal.local
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>

<Directory /var/www/henal.local>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    DirectoryIndex index.html
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/deltionkrant/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel debug

CustomLog ${APACHE_LOG_DIR}/deltionkrant/access.log combined

     Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

 </VirtualHost>

.htaccess normal web site:

RewriteEngine On
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-  mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos" [NC]
    RewriteRule ^$ http://m.henal.local/ [L,R=302]

Mobile web site:

<VirtualHost *:80>
ServerAdmin webmaster@henal.local
ServerName m.henal.local
ServerAlias henal.local

DocumentRoot /var/www/henal.local/mobile
<Directory />
    Options FollowSymLinks
    AllowOverride None
</Directory>
                         
<Directory /var/www/henal.local/mobile>
    Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    DirectoryIndex index.html
    Order allow,deny
    allow from all
</Directory>

ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
<Directory "/usr/lib/cgi-bin">
    AllowOverride None
    Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
    Order allow,deny
    Allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log

# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn

CustomLog ${APACHE_LOG_DIR}/access.log combined

     Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>

.htaccess mobile web site:

RewriteEngine On
RewriteCond %{HTTP_USER_AGENT} "!(android|blackberry|googlebot- mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos)" [NC]
RewriteRule ^$ http://www.henal.local/ [L,R=302]

Solution

  • Ok - so you are trying to do content negotiation here and you are regexing on the user agent. Aside from the rewrtile rules you have, you need to consider the following:

    1. Using the Apache Mobile Filter instead of a Regex. It is more accurate and provides a greater range of features
    2. You should allow users to choose the representation/experience that they want. So if you are presenting them with the mobile version, then you should let them choose the desktop version instead since this might actually have been what they wanted; and vice versa.
    3. This could be for a plethora of reasons e.g they have bookmarks to the desktop, someone shared a url to the desktop version etc. etc. Your content negotiation technique is pretty basic and doesn't take into consideration any of these use cases.

    Having considered this, specific to your problems above:

    Try removing the quotes from your RewriteCond patterns and add the R=302 and L flag to make sure now other rewrite directives are executed. i.e:

    RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|googlebot- mobile|iemobile|ipad|iphone|ipod|opera mobile|palmos|webos) [NC,R=302,L]
    

    Secondly its likely that you are seeing the default desktop "it works" because you are testing on a desktop browser and being redirected to the desktop host.