WordPress perlmalinks on lighttpd

I’ve moved my site in the last few days to a new dedicated server. I’ve decided to switch to Lighttpd. Now I can say that I’m fairly satisfied with the move.

But after I transfered the domain to the new server I’ve found out that none of my inner pages are accessible. The reason, as it turned up pretty quickly, is that the WordPress depends on Apache’s mod_rewrite to create the clean URLs (the so called permalinks).

Luckily, Lighttpd has its own mod_rewrite, however it operates a bit differently. The WordPress generated .htaccess has the following lines:

<IfModule mod_rewrite.c>;
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

This rules tells Apache to check if the requested URL is a file or directory that exist on the server, otherwise append the requested URL to index.php. The last statement can be written in Lighttpd’s mod_rewrite syntax as

^/(.+)/?$" => "/index.php/$1"

If we only use this rule, the posts will load up fine, but all static files will not be available, that includes. Apache handles this by checking if the requested URL exist as a file or directory. Unfortunately Lighttpd’s mod_rewrite has no way to check if a file exists. I’ve found out that a possible workaround for this is to use mod_magent. I use it to check if files exist and handle the clean URLs.

I’ve figured out that I shouldn’t just translate the rewrite rules, instead I need to adapt them. In Apache we check if a file exist in order to be able to serve the static files. The directories are checked so the server would be able to list them. The problem is now to achieve those objectives in Lighttpd. First we don’t really need to allow the directories to be listed (actually I’ve planed to block this option in my blog a long time ago). So the rule about the directories can be fully ignored. All the static files that need to be served have an extension, this includes all the files I’ve uploaded. We note that WordPress generated permalinks never have a dot (‘.’) in their URLs. So we can exploit this behavior to achieve our goal. The following condition uses regular expressions to check if a given url has an extension, and if so serves it.

^/(.*)\.(.+)$" => "$0"

The only difference from the Apache configuration is that the web-server itself returns the 404 message when a requested file is missing, instead of passing it to WordPress and letting it return the error.

To sum everything up, our mod_rewrite configuration should look like this:

url.rewrite = (
	"^/(.*)\.(.+)$" => "$0",
	"^/(.+)/?$" => "/index.php/$1"
)

There is no equivalent to .htaccess files in Lighttpd. So this configuration has to go directly into the main configuration file in /etc/lighttpd/lighttpd.conf (or where it exists in your server). Make sure you’ve enabled mod_rewrite, as this is required. You can enable it by putting the following line some where above the block.

server.modules += ("mod_rewrite")

If you have multiple sites hosted, you should use include the url.rewrite block inside a conditional statement. For example my configuration looks like this

$HTTP["host"] =~ "(www.)?domain.com" {
	url.rewrite = (
		"^/(.*)\.(.+)$" => "$0",
		"^/(.+)/?$" => "/index.php/$1"
	)

	[...other unrelated configurations...]
}

And I’ve enabled the mod_rewrite at the top of the /etc/lighttpd/lighttpd.conf file.

This works well for me.

10 comments

  1. To start earning money with your blog, initially use Google Adsense but gradually as your traffic
    increases, keep adding more and more money making programs to your site.
    thanks !! very helpful post!

  2. Simply discovered your web page through google and I consider this can be a disgrace that you are not ranked upper due to the fact that that is a fantastic post. To switch this I determined to avoid wasting your web site to my RSS reader and I will try to point out you in one of my posts since you actually deserv extra readers when publishing content material of this quality.

  3. An interesting discussion is worth comment. I think that you must write extra on this matter, it might not be a taboo subject but generally persons are not enough to speak on such topics. To the next. Cheers

  4. Good post. I study something more difficult on totally different blogs everyday. It’ll at all times be stimulating to learn content material from other writers and follow a little bit one thing from their store. I’d choose to make use of some with the content material on my weblog whether or not you don’t mind. Natually I’ll provide you with a link in your internet blog. Thanks for sharing.

  5. This is incredibly fascinating, You are a very skilled blogger. I have joined your feed and look forward to looking for more of the amazing write-up. Also, I’ve shared your web-site in my social networks!

Leave a Reply

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