I have setup my lighttpd in a fashion which redirects all subdomains to my primary domain in following fashion * .domain.com -> domain.com. Unfortunately when i type in a subdomain such as www.domain.com it goes directly to http://domain.com/site.fcgi/ and literally breaks the page. When i exclude the subdomain it opens the page perfectly.

Following lighttpd settings are used:

$HTTP["host"] =~ "\.domain\.com(:[0-9]+)?$" {
            url.redirect = ("^/(.*)" => "http://domain.com/$1")

$HTTP["host"] =~ "^domain\.com(:[0-9]+)?$" {    
   server.document-root = "/var/www/servers/domain.com/awesomesite"
   accesslog.filename = "/var/www/logs/domain.com/access.log"
   server.errorlog = "/var/www/logs/domain.com/error.log"
   fastcgi.server = (
       ".fcgi" => (
           "main" => (
               # Use host / port instead of socket for TCP fastcgi
               "bin-path" => "/var/www/servers/domain.com/awesomesite/domain.fcgi",
               "socket" => "/tmp/domain.sock",
               "check-local" => "disable",
    alias.url = (
        "/static/" => "/var/www/servers/domain.com/awesomesite/static/",
    url.rewrite-once = (
        "^(/static.*)$" => "$1",
        "^(/.*)$" => "/domain.fcgi$1",

Furthermore i have added FORCE_SCRIPT_NAME = '' to my settings.py.

I am not able to figure out where the problem is caused, there is nothing to come for in the log files either. Im a bit lost in no mans land.


url.rewrite-once triggers before url.redirect, and $HTTP["host"] =~ “domain\.com" matches www.domain.com too.

So it first rewrites (for example) http://www.domain.com/ internally to http://www.domain.com/domain.fcgi/ and then redirects the client to http://domain.com/domain.fcgi/. The client sends a new request which gets then rewritten to http://domain.com/domain.fcgi/domain.fcgi/ and then sent to the django app.

The solution is to make the second block match only "domain.com" and not the sub-domains, i.e. $HTTP["host"] == "domain.com" (simple comparison) or $HTTP["host"] =~ "^domain\.com" (anchored regular expression).

Even more strict regular expressions would be $HTTP["host"] =~ "\.domain\.com(:[0-9]+)?$" and $HTTP["host"] =~ "^domain\.com(:[0-9]+)?$"

  • I tried doing either of the second blocks as you described, but the behaviour is static. I even tried to delete the socks and reload, restart to be sure its not "old" data. Is there more i should do? – JavaCake Mar 14 '14 at 12:04
  • Looking at the access.log i see that when i go through a subdomain it attempts to acccess everything through the site.fcgi file insteader of / as it does with domain.com. – JavaCake Mar 14 '14 at 12:24
  • I have updated the config in my main question. – JavaCake Mar 14 '14 at 12:37
  • your config above uses a wrong quote. also use curl -v to test, and clear your browser cache. – Stefan Mar 14 '14 at 13:10
  • I have been clearing cache all day, but i just realised that the i had not ticked the right one off the list. But fortunately i have been using Safari aswell at the same time, and it started to show the right results. I appriciate your help! Thanks. – JavaCake Mar 14 '14 at 13:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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