A weird request, but I really need this answered. Can you guys help me using re-write rules to remove the trailing slash from urls in Lighttpd.conf?


**A** www.example.com/page/
**B** www.example.com/page

I want url A to be forced into the semantics of url B.

Thank you very much.


What you are trying to do sounds wrong. The web-server does not handle the request urls that the browser sees. This is an application issue. Your web-application needs to present the urls in the form of www.example.com/page instead of www.example.com/page/ that it does now.

If you do not want to change the application but still want this desired behaviour, you may want to consider using a filtering proxy in front of lighty and then filtering all the returned links.

  • The trouble is if you don't have a web application, and you're just serving static files. Then URLs are indeed entirely the server's responsibility. – Kodiologist May 17 '18 at 16:04

Not really to the point, but you might want to have the reverse effect...

Anyways, as far as lighttpd code goes, that would be something like this:

url.redirect = ( "/page/" => "/page" )
url.rewrite-once = ( "/page" => "/page/" )

but, since "/page" doesn't find a file and tries the directory "/page/" (and the rewrite seems to do nothing to hide that), that does produce a redirect-loop.

Then your real question would be: is it possible to tell lighttpd not to redirect on directories? That, unfortunately, I didn't find a way to do... but I didn't search in deep, in fact I do prefer URLs with trailing slashes (also visually)...


Suppose you want the request /foo/bar to serve /foo/bar/index.html, and /foo/bar/ to redirect to /foo/bar. Then you can say:

url.redirect = ("^/foo/bar/$" => "/foo/bar")
url.rewrite-once = ("^/foo/bar$" => "/foo/bar/index.html")

Rewriting directly to the index file instead of to /foo/bar/, and anchoring the redirect regex with $, avoids triggering the redirect rule after the rewrite.

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.