[Solved] Remove trailing slashes from input URLs
Added by Kodiologist almost 7 years ago
I'd like to strip trailing slashes from URLs and not add them, such that http://example.com/foo/bar/
redirects to http://example.com/foo/bar
, and the latter does not redirect to the former. But by default, lighttpd adds trailing slashes to URLs that point to directories.
This question was asked on Server Fault here: https://serverfault.com/questions/55315/remove-trailing-slash-lighttpd-url . The first answer says that one should accomplish this in one's web application code rather than lighttpd, but I'm not using a web application; I'm serving static files. The second answer merely points out that url.redirect = ( "/page/" => "/page" )
doesn't work because lighttpd still tries to add the trailing slash, creating a redirect loop.
I'm running lighttpd 1.4.49 on Ubuntu Server 16.04.
Replies (7)
RE: Remove trailing slashes from input URLs - Added by gstrauss almost 7 years ago
If no other handler declares that it will handle the request, then the filesystem handler checks if the file exists in the document root. If it is a directory, then lighttpd redirects the request to http://example.com/foo/bar/ with a trailing slash. On the subsequent request (from the redirect), again if no other handler is configured to handle the request, then mod_dirlisting will handle the request, as long as mod_dirlisting is activated.
What is handling the request in your case? If it is something like mod_fastcgi, mod_scgi, or mod_proxy, then those can be configured to handle the request prior to the filesystem handler.
.
Note: it would be more straighforward and more in line with lighttpd and other web servers if you simply handled the request as http://example.com/foo/bar/ with the trailing slash, rather than attempting to do it without.
RE: Remove trailing slashes from input URLs - Added by Kodiologist almost 7 years ago
What is handling the request in your case?
I'm using the filesystem handler, with index-file.names = ("index")
.
Note: it would be more straighforward and more in line with lighttpd and other web servers if you simply handled the request as http://example.com/foo/bar/ with the trailing slash, rather than attempting to do it without.
That's true, but it doesn't seem future-proof. If /var/www/foo/bar/
is a directory at the moment, that works fine, but if I later decide it doesn't need a whole directory and I make /var/www/foo/bar
an HTML file, then visiting http://example.com/foo/bar/
instead of http://example.com/foo/bar
will break any relative links on the page. In short, the trailing slash exposes an implementation detail that can cause trouble if the implementation changes.
RE: Remove trailing slashes from input URLs - Added by gstrauss almost 7 years ago
You are incorrect and seem to not understand PATH_INFO. Why don't you try going to http://example.com/foo/bar/baz/and/more and see what happens for your http://example.com/foo/bar/ site?
Anyway, please read the documentation for mod_rewrite
url.rewrite-once = ( "^/foo/bar$" => "/foo/bar/" )
RE: [Solved] Remove trailing slashes from input URLs - Added by Kodiologist almost 7 years ago
You are incorrect
I don't know why you don't believe me, so here's a demonstration. If you go to http://arfer.net/x/dt/foo , the page's relative link to an image works. If you go to http://arfer.net/x/dt/foo/ , the link is broken (because the browser tries to get http://arfer.net/x/dt/foo/img.png instead of the intended http://arfer.net/x/dt/img.png ).
Anyway, please read the documentation for mod_rewrite
url.rewrite-once = ( "^/foo/bar$" => "/foo/bar/" )
I've read this page, and I'm sorry if I'm being dense, but I don't see how I could use mod_rewrite
to remove the slashes. A rewrite only changes how the server treats the request, without changing what ends up in the user's address bar, right? What I'd like, to be clear, is to have http://example.com/foo/bar/
redirect to http://example.com/foo/bar
.
RE: Remove trailing slashes from input URLs - Added by gstrauss almost 7 years ago
Your web page is broken. One solution is to use root-anchored paths instead of relative paths. Another is to do something other than what you're doing on the server side. You are using mod_indexfile, which operates inside a directory, and you want it not to work that way. Your "denseness" is that you're asking for something that works exactly as designed (an index file inside a directory) to not be inside a directory. That's not how mod_indexfile works. Instead, you might try using your fubar links on your page, and send the link to http://example.com/foo/bar, which is internally rewritten (using the rule I gave above) to /foo/bar/, which will then be handled by mod_indexfile, if you have configured mod_indexfile.
RE: Remove trailing slashes from input URLs - Added by Kodiologist almost 7 years ago
Another is to do something other than what you're doing on the server side. You are using mod_indexfile, which operates inside a directory, and you want it not to work that way. Your "denseness" is that you're asking for something that works exactly as designed (an index file inside a directory) to not be inside a directory.
It's fine for it to be inside a directory on the real filesystem; the trouble is that this feature of the filesystem gets reflected in the URL. But if there's a way to accomplish this without mod_indexfile
, then by all means, I'm all ears.
Instead, you might try using your fubar links on your page, and send the link to http://example.com/foo/bar, which is internally rewritten (using the rule I gave above) to /foo/bar/, which will then be handled by mod_indexfile, if you have configured mod_indexfile.
I understand, but then how can I also get http://example.com/foo/bar/
to redirect to http://example.com/foo/bar
? Trying to do this seems to produce a redirect loop, perhaps because of mod_indexfile
.
RE: Remove trailing slashes from input URLs - Added by Kodiologist almost 7 years ago
I think I've figured it out:
url.redirect = ("^/foo/bar/$" => "/foo/bar") url.rewrite-once += ("^/foo/bar$" => "/foo/bar/index")
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.