Drupal & Lighttpd's Clean-URLs

This brief blog entry will document how to enable Clean-URLs in Drupal v5 using Lighttpd. Clean-URLs by default uses Apache's Mod_Rewrite and assumes the use of .htaccess located inside the local directory. Lighttpd does not premit pre-configured directories using .htaccess, so what now?

First thing to do is direct all 404 "Page Not Found" errors to Drupal's index.php. We do this by editing lighttpd's configuration file.

sudo vi /etc/lighttpd/lighttpd.conf

Add the following to enable mod_rewrite and direct all 404 errors to our installation directory (~/www/d/). Once added, reload the httpd.

[coolcode]server.modules = (
"mod_rewrite",
)
server.error-handler-404 = "/d/index.php"
[/coolcode]

sudo /etc/init.d/lighttpd reload

The next thing is to explain to drupal how to rewrite by editing the site's configuration (~/www/d/sites/default/settings.php).

vi ~/www/d/sites/default/settings.php

Add the following to the second to the last line, before the stop of php's ?>

[coolcode]if (empty($_GET['q'])) {
$query = strpos(request_uri(), '?');
$request= $query ? substr(request_uri(), 0, $query) : request_uri();
$path = substr($request, strlen(trim(dirname($_SERVER['SCRIPT_NAME']), '/'))+1);
if ($path != '/' && $path != '/index.php') {
$_GET['q'] = $path;
}
}[/coolcode]

fin.

Also See

Notes

This method has broken the pager feature.

Lighttpd & mod_magnet for Clean URLs

I've posted another way to get clean URLs with drupal and lighttpd using mod_magnet and a lua file I found on the web without having to modify the site's settings file.

Tags