Sysop:ApacheUserAgentAndProxy

Aus immerda
Zur Navigation springen Zur Suche springen

Apache user-agent detection behind a proxy

Configuration for a server side browser-dependant redirect based on mod_rewrite, that works well behind a proxy:

# Browserdetection
#
# detection is only done when getting /
<LocationMatch "^/$">
       # set this header to support user-agent 
       # detection through a caching proxy
       # since mod_rewrite does not do it for us
       # (s. http://stackoverflow.com/questions/3698363/mod-rewrite-not-sending-vary-accept-language-when-rewritecond-matches)
       # on < httpd 2.2.9
       # Header set Vary User-Agent
       # on httpd >= 2.2.9
       Header always merge Vary User-Agent
</LocationMatch>
#
# detect mobile useragents and redirect them to the mobile domain
RewriteEngine on
RewriteCond %{HTTP_USER_AGENT} (iphone|ipod) [OR]
RewriteCond %{HTTP_USER_AGENT} (android) [OR]
RewriteCond %{HTTP_USER_AGENT} (iemobile) [OR]
RewriteCond %{HTTP_USER_AGENT} (blackberry) [OR]
RewriteCond %{HTTP_USER_AGENT} (symbian) [NC]
RewriteRule ^/$ http://mobile.mywebpage.com [R=301,L]
#
# all other browsers we point to index.html
# so they are cached without the vary header set
RewriteRule ^/$ http://mywebpage.com/index.html [R=301,L]

Using that mechanism, we solve the following Problems:

  1. We have to tell the Proxy Server to Cache the redirection based on the User-Agent (As this is not done by default)
  2. Optimize the caching of the index-Site: As we need to cache / based on the user-agent (for redirection), we need to cache a lot of such requests, by redirecting / to index.html for every non-mobile Browser we will only cache redirects for everything and are able to cache /index.html with the usual Vary Headers, which will usually lead to much less cached versions of index.html