$ cat /dev/brain > /dev/blog

Life is written in chapters but the table of contents is missing.

29 Nov

WordPress on lighttpd: Rewrite rules with WP Super Cache and WPtouch


As a few of my readers already know, this weblog is run by a WordPress 2.x installation on a lighttpd web server (on top of a Debian Linux host). I have also installed, among others, the following plugins for WordPress:


  • WP Super Cache — Serves a cached HTML version of WordPress pages instead of serving each page as PHP output for every single request. This speeds up loading time and reduces the web server load. A lot!
  • WPtouch — When a user is using a mobile device, e.g., an iPhone, iPod touch, Android phone etc., they will see a slightly different layout of this blog, particularly optimized for small screen sizes.

lighttpd does not understand Apache’s RewriteRules that are used for permalinks in WordPress. Therefore, you’ll have to come up with your own set of rewrite rules. Based on the rewrite.lua script initially written by Giovanni Intini, I have come up with a slightly modified version that takes the presence of the WPtouch plugin into account.

To install this script, perform the following steps:

  1. Download the latest version of the rewrite.lua script from my github project site.
  2. Place rewrite.lua in the root directory of your WordPress installation, i.e., the same directory where you find the WordPress PHP files such as wp-config.php.
  3. Update your lighttpd config file for the host running WordPress and include this command:
    magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )1
  4. Make sure the Mobile device support option of the WP Super Cache plugin is enabled and the Rejected User Agents list is up-to-date. You’ll find those options in the Settings -> WP Super Cache menu of WordPress once WP Super Cache has been activated.

The lighttpd host configuration should now look somewhat similar to this:

$HTTP["host"] == "myblog.com" {
server.document-root = “/srv/www/myblog/wordpress”
accesslog.filename = “/var/log/lighttpd/
myblog/access.log"
magnet.attract-physical-path-to = ( server.document-root + “/rewrite.lua” )
}

Restart lighttpd to put the changes in place. Check your error log for any strange entries. Check the WP Super Cache settings of your blog and try to view it from a mobile device or an iPhone simulator, e.g., iPhoney.

Let me know if you’d like to further improve the rewrite.lua code. Just go ahead, clone it from the github repository and start working on it! If you’re interested in writing your own LUA code check out the Programming in Lua book or the Programming in LUA website.

  1. This requires ModMagnet being set up for your lighttpd server. Follow the previous link if you need help installing it. []


6 Responses to “WordPress on lighttpd: Rewrite rules with WP Super Cache and WPtouch”

  1. Chrische on Apr 25, 2010 | Reply

    Nice Lua Script. I did it slightly different without lua: http://www.chrische.de/2010/04/lighttpd-in-verbindung-mit-wordpress-und-rewrites/

  2. Jean Pierre on Apr 25, 2010 | Reply

    @Chrische: I like your solution. It makes it easy to include the config file for several virtual hosts.

    In our case, there was no easy way to cover our requirements (caching!) using url.rewrite-once so we switched from configuring the rewrites in lighttpd-*.conf to the host-based LUA solution — which works great, BTW.

  3. Ben White on Dec 9, 2010 | Reply

    Thanks very much for this. Fixed all the issues we were having with WPtouch (not loading new entries, always falling back to the main site when clicking through to a post).
    Appreciate your time and effort :D

  4. Ben White on Dec 9, 2010 | Reply

    Only one issue reported back so far – problems as described above are still occurring with the Blackberry Torch (blackberry9800).

    I’ve made changes to the Super Cache and WPtouch plug-ins, so I am wondering if there are any changes required within rewrite.lua?

    Thanks,
    Ben

  5. Jean Pierre on Dec 15, 2010 | Reply

    Ben: Thanks for reporting this issue.

    I checked the rewrite.lua file — it does contain the User-Agent string used by BlackBerry phones, so it should work. I can’t test as I don’t have access to a BlackBerry (or a BlackBerry simulator), though.

  6. Ben White on Dec 22, 2010 | Reply

    Thanks Jean, it appears to have been a cache issue on the user end – thanks for the response :) Everything works!

Post a Comment