Removing trailing slash using Lighttpd

If you are deploying your PHP website using lighttpd, you may need to be able to remove the ending slash from an url. E.g. http://www.example.com/my-url/
should redirect to: http://www.example.com/my-url

Regardless of which web framework you are using, this is perhaps best done using your web server. There is a plethora of information about Apache out there, but here is a snippet of how to do it using my server of choice, lighttpd:

$HTTP["host"] == "www.example.com" {
        url.redirect-code = 301
        url.redirect = ("^index.php(.+)/$" => "$1")

        server.document-root = "/var/www/public"
        url.rewrite-if-not-file = (
                "^/(.*)$" => "index.php/$1",
        )
}

The gotcha here is that rewrite rule is executed first, and redirect afterwards. This means that you have to take into account index.php (or app.php, or whatever you are rewriting to) when creating your redirect rule.