0

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/"
}
mgorven
  • 30,036
  • 7
  • 76
  • 121

2 Answers2

0

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

127.0.0.1  myhost1.com
127.0.0.1  otherhost.com

reload lighttpd

rc-service -vv lighttpd restart
0
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.

Stefan
  • 819
  • 1
  • 7
  • 18
  • 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 07: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 07:55