I've set a simple virtual hosting using Lighttpd simple_vhost module and want to do a permanent 301 redirect from www to non-www.

Here are most significant parts of my configuration file:

server.modules = (

simple-vhost.server-root = "/Websites/"

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

The redirect block is taken as-is from the Lighttpd documentation, but it doesn't work at all. There is subtle difficulty mentioned on the simple_vhost module docs:

You have to keep in mind that conditionals and simple-vhost interfere with one another.

I've tried the solution proposed there (http://redmine.lighttpd.net/projects/1/wiki/Docs_ModSimpleVhost), but still without success:

$HTTP["host"] != "www.example.org" {
    simple-vhost.server-root = "/Websites" 
    simple-vhost.default-host = "example.org"  

$HTTP["host"] == "www.example.org" {
    server.document-root = "/Websites/example.org" 

What is the proper way to do redirects from www to non-www when using virtual hosts?

Here is my full config file.

server.modules = (

server.follow-symlink = "enable"

server.document-root = "/Websites"

server.errorlog = "/Websites/error.log"
server.breakagelog = "/Websites/breakage.log"

index-file.names = ("index.html")

accesslog.filename = "/Websites/access.log"

## deny access the file-extensions
# ~ is for backupfiles from vi, emacs, joe, ...
# .inc is often used for code includes which should in general not be part
# of the document-root
url.access-deny = ( "~", ".inc" )

# which extensions should not be handle via static-file transfer
# .php, .pl, .fcgi are most often handled by mod_fastcgi or mod_cgi
static-file.exclude-extensions = ( ".php", ".pl")

server.port = 80
server.bind = ""

## to help the rc.scripts
server.pid-file = "/var/run/lighttpd.pid"

## virtual directory listings
dir-listing.encoding = "utf-8"
server.dir-listing = "enable"

server.username = "lighttpd"
server.groupname = "lighttpd"

#### compress module
compress.cache-dir = "/var/cache/lighttpd/compress/"
compress.filetype = ("text/plain", "text/html", "application/x-javascript", "text/css")

#### external configuration files
## mimetype mapping
include_shell "/usr/share/lighttpd/create-mime.assign.pl"

## load enabled configuration files,
## read /etc/lighttpd/conf-available/README first
include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

# Simple virtual host.
$HTTP["host"] != "www.example.com" {
simple-vhost.server-root = "/Websites/"

$HTTP["host"] == "www.example.com" {
server.document-root = "/Websites/example.com/"
  • Can you please restart your lighttpd service and post the error you might see in the error logs.
    – Napster_X
    Dec 24, 2012 at 8:06
  • Also, would really help if you can paste a link to the complete lighttpd file ... may be use something like pastebin/
    – Napster_X
    Dec 24, 2012 at 8:10
  • I've updated the post with a link to full configuration file. Unfortunately, there are no errors in the error.log. Dec 24, 2012 at 8:42
  • What happens when you try to browse you site with url www.example.com ... or is it possible, that you can share your site name, if it's public
    – Napster_X
    Dec 24, 2012 at 9:31
  • I use the HTTP Client Mac app to load the www.-prefixed page and it prints just "(( Zero-length response returned from server. ))". Dec 27, 2012 at 7:18

2 Answers 2

simple-vhost.default-host = "example.org"

will already take care of the www.example.org subdomain (unless the folder for it exists).

both config snippets look good otherwise; but you didn't paste the complete config, the includes are missing. the mime types are probably not important, but

include_shell "/usr/share/lighttpd/include-conf-enabled.pl"

can do many things.

lighttpd -p -f /etc/lighttpd/lighttpd.conf

shows the complete config.

also, as the comments indicated, tell us how you tested (what does "doesn't work at all" mean?). test with curl to make sure your browser cache isn't the problem.

have a look at http://redmine.lighttpd.net/projects/lighttpd/wiki/DebugVariables too.

  • The problem is that I have to host multiple websites simultaneously and each website should be accessible via both "example.com" and "www.example.com". I've used HTTP Client Mac app to access the page and it just printed "Zero-length response returned from server.". Curl gave me another error (* Could not resolve host: www.example.com; nodename nor servname provided, or not known) and I think my issue can be even unrelated to the Lighttpd itself. Dec 27, 2012 at 7:29
  • I've fixed my domains zone file, hope everything is going to be OK, when the changes propagate. Thanks! Dec 27, 2012 at 7:55

this steps work on alpine linux

load fine laravel, slim and codeigniter

uncomment:       "mod_rewrite",
uncomment:       "mod_simple_vhost",

var.basedir  = "/var/www/localhost"
server.document-root = var.basedir + "/htdocs"

the next 3 lines make the magic!

simple-vhost.server-root = "/var/www/localhost/htdocs/vhost"
simple-vhost.default-host = "myhost1.com"
simple-vhost.document-root = "/public"

config rewrite

url.rewrite-if-not-file = ("(.*)" => "/index.php/$0")

for each host create a folder for example:

myhost1.com    /var/www/localhost/htdocs/vhost/myhost1.com
otherhost.com    /var/www/localhost/htdocs/vhost/otherhost.com

add your host to /etc/hosts file  myhost1.com  otherhost.com

reload lighttpd

rc-service -vv lighttpd restart

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .