05 Apr 2008

Lighttpd Rewrite Vs Redirect

A recurring source of confusion for Lighttpd users is the interaction between mod_rewrite and mod_redirect.

Mod_rewrite always takes precedence over mod_redirect, so if a mod_redirect needs to be executed before a mod_rewrite, they must be constrained to act on mutually exclusive URLs.

For example if a www URL needs to be directed to the non-www one, the code would be

$HTTP ["host"] =~ "^www\.(.*)" {
  url.redirect = ( "^/(.*)" => "http://%1/$1" )

$HTTP ["host"] !~ "^www\." {
  # rewrite rules go here.

In this case, the mod_rewrite would be executed only after the mod_redirect has stripped off the www from the URL.