Take the 2-minute tour ×
Server Fault is a question and answer site for professional 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 = (
  "mod_simple_vhost",
  "mod_redirect",
)

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 = (
"mod_access",
"mod_simple_vhost",
"mod_alias",
"mod_accesslog",
"mod_compress",
"mod_redirect",
"mod_cgi"
)

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 = "10.20.30.40"

## 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/"
}
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 www.example.com ... 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 = "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.

share|improve this answer
    
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. –  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

 
discard

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.