Drupal Clean URL with Lighttpd

I found few url-rewrite based solution for enabling CleanURL in Drupal + Lighttpd but none of them worked flawlessly. Guys, all we need here is a simple equivalent of this code (without any side effect, like 404 header etc).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

So I continued searching for the perfect solution, and here is what I found. It works like charm on Drupal5 and Drupal6 :-) Another victory for KISS! (keep it sweet and simple)

Content of /etc/lighttpd/lighttpd.conf

$HTTP["host"] =~ "dev\.sudhaker\.com$" {
  server.document-root = "/shared/sites/htdocs_drupal5"
  magnet.attract-physical-path-to = ( server.document-root + "/rewrite.lua" )
}

Please make sure mod_magnet is enabled :-)

And content of $DRUPAL_ROOT/rewrite.lua

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

if (not attr) then
  lighty.env["uri.query"] = "q=" .. lighty.env["uri.path"]
  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

Note: It only works on lighttpd 1.4.2+ ;-)

Cheers,
Sudhaker
Ref: full article

Comments

timl’s picture

Thanks, I tried a few different approaches and this was the first to work for me!

zanlus’s picture

This worked really well. I would note two things:

There is a line in lighttpd.conf that looks like it should be uncommented:
#### url handling modules (rewrite, redirect, access)
#url.rewrite = ( "^/$" => "/server-status" )

Fight the urge! Uncommenting this line caused the site to load correctly on every page except the front page.

The second thing to note is that if you are running multiple sites, placing this in the drupal directory isn't very efficient. Instead, I put mine in the same directory as lighttpd.conf and changed the link in my HTTP ["host"] directive with a static path.

Ross

thekevinday’s picture

FYI

lighttpd's prior to version 1.4.20 have a bug in their URL rewrite code that caused the decode url to fail.

See: http://trac.lighttpd.net/trac/ticket/1720

So, you will need at least lighttpd-1.4.20 for URL rewrite with drupal.

And if you think 1.4.19 works, watch out for a URL rewrite memory leak: http://trac.lighttpd.net/trac/ticket/1775

Jayson Wonder’s picture

How do we do this now considering after 1.4.24 mod_magnet and lua is not needed. I understand we can use a lighttpd rewrite rule. Problem is what rule must be used.

Many functions I use on my drupal site give me this error:

The requested page "/batch?op=start&id=41" could not be found

I am currently using this rule:

url.rewrite-if-not-file = (
"^/(.*)$" => "/index.php?q=$1")

Not sure if this is why.

Any thoughts are appreciated!