Maintenance mode (HTTP 503) with lighttpd and PHP

When you put your website in maintenance mode, it’s a good idea to return a HTTP 503 error code to the client.

This code indicates that “the server is currently unable to handle the request due to a temporary overloading or maintenance of the server”.

The 503 code is used to avoid crawlers or caching proxy use the maintenance page as the new valid content for the request. You certainly don’t want Google save this content in his search index as the content of your website :)

To achieve this we will use a rewrite rule in lighttpd to redirect all requests to a single PHP script which will return a 503 error code and print an informative message.

Lighttpd configuration

url.rewrite = ( "" => "/maintenance.php" )

This configuration will redirect any request to maintenance.php script.

If you need to serve an image in your maintenance page, you have to add another rule to the rewrite process like that :

url.rewrite = ( "upgrading.png" => "$0",
                "" => "/maintenance.php" )

Let some users see the website

It might be usefull to let admins or developers access the website during the maintenance.
For that, you can disable the maintenance rewrite rule for certain IP addresses :

 $HTTP["remoteip"] != “192.168.1.42″ {
               url.rewrite = ( "upgrading.png" => "$0",
                      "" => "/maintenance.php" )
}

PHP code example

<?php
header("HTTP/1.1 503 Service Unavailable");
?>
We're currently upgrading our servers...

Links

This entry was written by CharlyBr, posted on July 22, 2008 at 2:28 am, filed under http and tagged , . Leave a comment or view the discussion at the permalink.

Permanent redirect (301) with lighttpd

If you want to redirect visitors that access your website without the ‘www’, you can use mod_redirect with the following syntax:

$HTTP[”host”] =~ “^lescampeurs\.org” {
    url.redirect = (
        ”^/(.*)$” => “http://www.lescampeurs.org/$1″
    )
}

Links:

This entry was written by CharlyBr, posted on June 30, 2008 at 11:47 pm, filed under http and tagged . Leave a comment or view the discussion at the permalink.

Deny access to .svn directories with Apache2 / lighttpd

If you’re using SVN to control your web application, your certainly need to deny access to .svn sub-directories.

With Apache2

    <DirectoryMatch "^/.*/\.svn/">
            Order allow,deny
            Deny from all
    </DirectoryMatch>

With lighttpd

    $HTTP["url"] =~ "/\.svn/" {
        url.access-deny = ( "" )
    }

This entry was written by CharlyBr, posted on May 28, 2008 at 7:17 am, filed under http and tagged , , . Leave a comment or view the discussion at the permalink.

Use mod_redirect with lighttpd

Enable mod_redirect

server.modules += ( "mod_redirect" )

Force your domain with www.

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

Force your domain without www.

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

Redirect HTTP requests to HTTPS

$SERVER["socket"] == ":80" {
    $HTTP["host"] =~ "(.*)" {
        url.redirect = ( "^/(.*)" => "https://%1/$1" )
    }
}

Redirect an old domain to your new domain

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

Redirect a url that can be matched by a mod_rewrite rule

Lets say that you want to redirect http://www.domain.com/party to http://blog.domain.com/party while you have a mod_rewrite rule on www.domain.com that redirect all URLs to a controller (“^(.*)$” => “/index.php$1″).

Your redirect will not work because mod_rewrite always execute before redirect rules. To bypass this, you need to use the $0 as the rule target to pass URLs through unmangled.

e.g.

url.redirect = ( "^/party(.*)" => "http://blog.domain.com/party$1" )
url.rewrite-once = ( "^/party" => "$0" )

tested with lighttpd 1.4.x

This entry was written by CharlyBr, posted on May 6, 2008 at 12:56 pm, filed under http and tagged . Leave a comment or view the discussion at the permalink.