Sign up ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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 (, but still without success:

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

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

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 = "/var/run/"

## 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/"

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

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

$HTTP["host"] == "" {
server.document-root = "/Websites/"
share|improve this question
Can you please restart your lighttpd service and post the error you might see in the error logs. – GeekRide Dec 24 '12 at 8:06
Also, would really help if you can paste a link to the complete lighttpd file ... may be use something like pastebin/ – GeekRide Dec 24 '12 at 8:10
I've updated the post with a link to full configuration file. Unfortunately, there are no errors in the error.log. – Konstantin Pavlikhin Dec 24 '12 at 8:42
What happens when you try to browse you site with url ... or is it possible, that you can share your site name, if it's public – GeekRide Dec 24 '12 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. ))". – Konstantin Pavlikhin Dec 27 '12 at 7:18

1 Answer 1

simple-vhost.default-host = ""

will already take care of the 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/"

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 too.

share|improve this answer
The problem is that I have to host multiple websites simultaneously and each website should be accessible via both "" and "". 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:; nodename nor servname provided, or not known) and I think my issue can be even unrelated to the Lighttpd itself. – Konstantin Pavlikhin Dec 27 '12 at 7:29
I've fixed my domains zone file, hope everything is going to be OK, when the changes propagate. Thanks! – Konstantin Pavlikhin Dec 27 '12 at 7:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.