How to combine Lighttpd simple_vhost with www to non-www redirects

lighttpdredirectvirtualhost

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

Best Answer

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.

Related Topic