0

Is it possible to redirect all *.domain.com to my domain.com?

I have been messing around with the Regex but without any luck:

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

$HTTP["host"] =~ “domain\.com" {    
   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",
    )     
}

Basically something goes wrong with this solution as it tends to kill all the files after the training slash that has a dot.

JavaCake
  • 111
  • 3
  • 10
  • You should match `$HTTP["host"] == "domain.com"`, or use an anchored regex, as otherwise it will first rewrite and then redirect the rewritten url. – Stefan Mar 14 '14 at 08:22
  • @stefan, could you kindly make an answer with your proposal, as i am not completely sure i understand you. I have made an additional question, hence something goes wrong in the redirect: http://serverfault.com/questions/581947/redirect-with-lighttpd-and-fastcgi-on-django-app – JavaCake Mar 14 '14 at 09:04

1 Answers1

2

This should do the job, I think:

server_modules = (
                      # your modules
                      "mod_redirect",
                  )

$HTTP["host"] =~ ".*\.example\.com" {
    url.redirect = ( "^/(.*)" => "http://example.com/$1" )
}
  • Something is wrong, its looping my `$HTTP["host"] =~ "domain\.com" {` – JavaCake Mar 13 '14 at 14:47
  • And i get bad request with subdomains `xxx.domain.com` – JavaCake Mar 13 '14 at 14:52
  • Updated with an explicit reference to mod_redirect, and tested on a simple host. Should be OK now. –  Mar 13 '14 at 15:00
  • I tried this, its still looping. for some reason.. – JavaCake Mar 13 '14 at 15:07
  • It seem to be redirecting, but when it redirects from a subdomain it breaks my aliases for some reason. I dont understand why. The previous problem was for some reason my caching. – JavaCake Mar 13 '14 at 15:10
  • So when i enter from `domain.com` everything is fine, until i do `www.domain.com` it redirects, but then it messes the site completely up. Its important to mention that im using Django, so its deplying the site with fcgi. – JavaCake Mar 13 '14 at 15:25
  • Sounds like you need to use `curl` or similar to see precisely what is happening with the redirection. But I think the code above is correct, and any remaining problems will be Django-issues. –  Mar 13 '14 at 15:26
  • I have looked everything through and i cant really spot the problem. I will try to curl and report back. – JavaCake Mar 13 '14 at 18:14
  • I have posted complete code for review! Also added your changes. – JavaCake Mar 13 '14 at 18:30