Apache2 - 301 Redirect when missing "/" at the end of directory in the url
Solution 1:
Sending the data without a redirect would break relative links. If http://server/directory
contains file
, then the full URL for that would be http://server/directory/file
. A link specified like <a href="file">
will point to http://server/directory/file
if the base URL is http://server/directory/
, but if the base URL was only http://server/directory
it would point to http://server/file
instead, which is not the intended result.
Apache could have generated the directory listing in two different ways depending on the URL instead of redirecting. However that would not work if there was an index.html
file in the directory. So instead Apache is using the approach, that works in both cases.
This is not a new behavior, one decade ago Apache was behaving the same way. Clients which cannot handle a redirection should have been fixed by now. But for any clients who cannot handle a redirection, Apache should be sending along a tiny html file with a link that can be followed instead.
Solution 2:
mod_dir
adds this redirect, and you can disable it with a DirectorySlash Off
directive.
Note, however, that disabling the trailing slash redirect could cause some pages to break. If the page being returned contains relative links, then those URLs would resolve differently if the page is served without the trailing slash.