WordPress Pretty Permalinks on Lighttpd

At some point in your WordPress-life, you need a pretty permalink (name of the link at the navigation bar). On Apache we can easily use the rewrite module (mod_rewrite) which works pretty well. On the other hand, I found it confusing to make the equivalent rewrite rule on Lighttpd, therefore I use the other module called magnet module (mod_magnet) instead. And these are some simple steps how to use it:

Install Magnet Module

Simply install it from the default repositories.

sudo aptitude install lighttpd-mod-magnet

Dont forget to enable the module:

sudo lighty-enable-mod magnet

Lighttpd Configuration

Edit the configuration file:

sudo vi /etc/lighttpd/lighttpd.conf

If you do not use vhosts, add the following to the end of the file:

magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )

If you do use vhosts, add that line to your vhost instead:

$HTTP["host"] =~ "(^|\.)example\.com" {
server.document-root = "/var/www/example.com"
magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )
}

Where example.com is the name of your website and /var/www/example.com is the directory where your website is.

Make a Lua script, assuming that Lua is supported by default (I didn’t have to install anything at that time):

sudo vi /var/www/example.com/rewrite.lua

Again, don’t forget change /var/www/example.com to your own setting.

Add the following Lua script into the file, thanks to Sudhaker.

attr = lighty.stat(lighty.env["physical.path"])

if (not attr) then
lighty.env["uri.path"] = "/index.php"
lighty.env["physical.rel-path"] = lighty.env["uri.path"]
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end

Reload the module:

sudo /etc/init.d/lighttpd force-reload

Now visit your WordPress’s permalinks configuration, http://example.com/wp-admin/options-permalink.php and use the default or follow this guide. Enjoy!

This entry was posted in how-to and tagged , , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

6 Comments

  1. Viðarr
    Posted 09.01.2011 at 03:10 | Permalink

    Why not use mod_rewrite in lighttpd? The linked guide even links to a blog post elsewhere which provides the steps for lighttpd.

  2. Viðarr
    Posted 09.01.2011 at 03:15 | Permalink

    Also see the history of the implementation file:

    http://redmine.lighttpd.net/projects/lighttpd/repository/revisions/2647/changes/branches/lighttpd-1.4.x/src/mod_rewrite.c

    mod_rewrite is way old within lighttpd …

  3. Posted 13.01.2011 at 15:30 | Permalink

    Hi Viðarr,

    Thanks for the comment. I tried mod_rewrite in Lighttpd a few years ago while I was configuring a web server. However, I found some issues with the rewrite rules for a server with many virtual hosts. It was more obvious in LUA and mod_magnet worked pretty well. This is very flexible and robust in many server configurations.

    Unlike Apache, WordPress still does not provide “standard” rewrite rules for Lighttpd (and probably never will).

    The guide links to an article with user-made rewrite rules (which might not work in some configurations), but that article also links to a more general solution with mod_magnet from sudhaker, in which I adapted from.

  4. Posted 10.05.2011 at 01:20 | Permalink

    +1
    Just what I was looking for!

  5. Posted 11.05.2011 at 01:12 | Permalink

    I am making the big move from Apache to Lighttpd. The rewrites, are very confusing to me. Furthermore, if I don’t get them correct the first time, it could hurt me as far as SEO.

  6. Posted 11.05.2011 at 14:47 | Permalink

    @Spunky Jones You can run lighttpd on port 88 and test it until you’re quite confident then swap it on.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>