lighttpd + WordPressMU (english)

For those who still don’t know it, lighttpd is a cool web server: small, efficient, extensible, powerful… but just as with any other new server, you’ve got to get used to it and doing things “its way” instead of “as always”.

One thing that’s somewhat different in Apache and lighttpd is “mod_rewrite”: lighttpd has its own URL-rewriting module, with a syntax slightly different to that of Apache’s mod_rewrite, but lighty’s rewrite doesn’t support checking if a file exists and doing things accordingly (-f and -d in Apache). WPMU depends on this. Fortunately, lighttpd has a tool more powerful than mod_rewrite: mod_magnet.

mod_magnet is a module which allows us to do WHATEVER WE WANT with a query before it’s served by the rest of modules (PHP…), by using the LUA programming language. Initially called mod_cml, this module was used just as an advanced caching device, but in recent releases (>=1.4.12) it’s evolved into a way to hack at the http server’s very core.

Enough talk, let’s get our hands dirty. ;-) Apache’s .htaccess for this blog would look like:
[code lang="apache"]
RewriteEngine On
RewriteBase /

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteRule ^(.*/)?files/(.*)
wp-content/blogs.php?file=$2 [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*.php)$ $2 [L]
RewriteRule . index.php [L]
[/code]

There are two issues here:

  • The lines:
    [code lang="apache"]RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d[/code]
  • LUA doesn’t seems to support catches with the ? repetition like in (…..)?, so we’ve got to come up with another regex to get rid of the first “folder” on the URLs.

And the winner regexp :-P for this second problem is:
[code]"^/?([^/]*/?)files/$"[/code]
For the first problem, you can take a look at the LUA script I’m using on this blog here: wpmu.lua. In order to use it, you’ve to add something like this to the lighttpd.conf file:

[code]$HTTP["host"] =~ "blogs.domain.com" {
magnet.attract-physical-path-to = ( "/PATH/wpmu.lua" )
}[/code]

I’m sure there’re better ways to do this. I don’t quite like that regexp nor the LUA code, but hey, it works. :)

[code lang="lua"]
if (not lighty.stat(lighty.env["physical.path"])) then
if (string.match(lighty.env["uri.path"], "^(/?[^/]*/)files/$")) then
lighty.env["physical.rel-path"] = "index.php"
else
n, a = string.match(lighty.env["uri.path"], "^(/?[^/]*/)files/(.+)")
if a then
lighty.env["physical.rel-path"] = "wp-content/blogs.php"
lighty.env["uri.query"] = "file=" .. a
else
n, a = string.match(lighty.env["uri.path"], "^(/?[^/]*/)(wp-.*)")
if a then
lighty.env["physical.rel-path"] = a;
else
n, a = string.match(lighty.env["uri.path"], "^(/?[^/]*/)(.*\.php)$")
if a then
lighty.env["physical.rel-path"] = a
else
lighty.env["physical.rel-path"] = "index.php"
end
end
end
end
lighty.env["physical.path"] = lighty.env["physical.doc-root"] .. lighty.env["physical.rel-path"]
end
[/code]

UPDATED 20070411:

  • Fixed a bug when accessing image files (/file/YEAR/MONTH/…)
  • Reworked the regexps

Please test and report success. I’m new to WPMU so I’m stumbling upon errors as I go. :)

  • Twitter
  • Facebook
  • Meneame
  • email
  • Print
  • PDF
  • RSS

Artículos relacionados (o no):

  • Pues no hay. :-(

10 Responses to “lighttpd + WordPressMU (english)”


Leave a Reply




Bear
Creative Commons Attribution-NonCommercial 2.5 Spain
This work is licensed under a Creative Commons Attribution-NonCommercial 2.5 Spain.