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/
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 thoughts on “WordPress on lighttpd: Rewrite rules with WP Super Cache and WPtouch”

  1. @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.

  2. 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 😀

  3. 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?


  4. 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.

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

Leave a Reply

Your email address will not be published. Required fields are marked *