Home » Linux System Administration » Removing trailing slash using Lighttpd

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.