Tuesday, January 18, 2011

lighttpd rewrite rules for WordPress

I am a fan of pretty, easy-to-remember URLs (see your address bar). WordPress, the software this blog runs on, allows for such things, but it requires you to make use of so-called rewrite rules on the web server end.
For Apache users, those rules are supplied with WordPress, and it can even set them up for you automatically in some cases. I, however, use lighttpd, not Apache. WordPress does not come with rewrite rules for lighttpd, so I turned to Google. I don’t remember exactly how it transpired, but I recall most of the example rules I found being broken in one way or another, so I fixed some of them up and came up with ones that work perfectly (as far as I know). I hope they can be useful to others, so here goes.

Note: These have been tested with versions 2.3 and 2.5. I’m not sure how well they work with past or future releases.
First, make sure that the mod_rewrite module is loaded.

Single-user WordPress (probably what you want)

Update: Here is a more general solution -> http://sudhaker.com/2008/04/wordpress-permalinks-lighttpd/

Create a file called /etc/lighttpd/wp-rewrite.conf with the following contents:
url.rewrite-once = (
    "^" + wpdir + "(wp-.+).*/?" => "$0",
    "^" + wpdir + "(sitemap.xml)" => "$0",
    "^" + wpdir + "(xmlrpc.php)" => "$0",
    "^" + wpdir + "keyword/([A-Za-z_0-9-])/?$" => wpdir + "index.php?keyword=$1",
    "^" + wpdir + "(.+)/?$" => wpdir + "index.php/$1"
)
Then, in your /etc/lighttpd/lighttpd.conf, you should have something similar to this:
$HTTP["host"] =~ "blog\.nix\.is" {
    var.wpdir = "/"
    include "wp-rewrite.conf"
}
If you installed WordPress in some place other than the web root (e.g. /blog/), then adjust var.wpdir accordingly.

Multi-user WordPress (mu.wordpress.org)

Source: http://mu.wordpress.org/forums/topic.php?id=3901

Create /etc/lighttpd/wpmu-rewrite.conf with the following contents:
server.error-handler-404 = "/index.php"
url.rewrite-once = (
    "^/(.*/)?files/$" => "/index.php",
    "^/(.*/)?files/(.*)" => "/wp-content/blogs.php?file=$2",
    "^(/wp-admin/.*)" => "$1",
    "^/([_0-9a-zA-Z-]+/)?(wp-.*)" => "/$2",
    "^/([_0-9a-zA-Z-]+/)?(.*\.php)$" => "/$2",
)
Then, in lighttpd.conf:
$HTTP["host"] =~ "domain\.com" {
    simple-vhost.default-host = "domain.com"
    include "wpmu-rewrite.conf"
}
This might be slightly different depending on your vhost setup, but you get the idea.

0 comments:

Post a Comment

 
Design by GURU