I haven't really noticed this Redirect(301) when requesting a url like this without slash("/") at the end: http://server/directory

The server will respone with a 301 Redirect Permanent header with a Location header locating to http://server/directory/.

See this live example:

User Request:

GET /social HTTP/1.1
( )

Apache Server Responce:

HTTP/1.1 301 Moved Permanently

User Request:

GET /social/ HTTP/1.1
( )

Apache Server Responce:

HTTP/1.1 200 OK

Apache access.log: - - [05/Apr/2014:22:06:47 +0200] "GET /social HTTP/1.1" 301 558 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0"
- - - [05/Apr/2014:22:06:47 +0200] "GET /social/ HTTP/1.1" 200 942 "-" "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:27.0) Gecko/20100101 Firefox/27.0"

The /social/ directory contains an index.html file.

Apache Software: Apache/2.2.22 (Ubuntu)
Directory Options: Options Indexes FollowSymLinks MultiViews

So, my question is: Why is apache doing this? And how to prevent the redirect and send out the index.html directly? Clients have to send two requests which is really unnecessary. And maybe some of the clients doesn't allow Redirects and will not be able to go to the site without the ending slash ("/").

I don't want to disable the redirect. I wan't the server to send out the response directly without any redirect. Even when requesting /social.

Is apache designed to redirect those requests? The server could just send the data without redirecting, right? Should I use the mod_rewrite to prevent this? Or another configuration? Or should I just let it be like this and add a slash at the end of all html links and live with some redirects?

What do you guys think?

  • I think you will have to clarify why you would want to disable this. The only sane alternative to the redirect would be responding with 404 for /social in your example. – Håkan Lindqvist Apr 6 '14 at 9:33
  • I don't want to disable it, just prevent the redirect. I would like the server to output the index.htmlwithout redirect – Jonathan Gurebo Apr 6 '14 at 9:42
  • By "this" I was referring to the normal behavior with a redirect from the incorrect url to the correct url. See the answer from @kasperd for some of the reasons why it would be a bad idea to serve the content from eg /social. (Also, just in general, serving the same content at multiple urls is usually not desired.) – Håkan Lindqvist Apr 6 '14 at 9:49
  • Did you ever solve this problem? The accepted answer does not solve the problem, it only explains "why" it exists. – emmdee Jul 8 '20 at 20:23

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.

  • 1
    This does not answer the question. It is correct, that relative links might get broken. But if someone wants to deliver content for a path without a trailing slash, then they might have resaons for that, for example when a POST request is sent to a webservice. There are no relative links in webservices, and a redirect will cause the POST body to get lost. – dr fu manchu Oct 12 '17 at 10:26

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.

  • Hello from the future. I'm a lifelong nginx user and just hit this with Apache and no idea how to solve it. For SEO reasons I want to limit the amount of 301's. Apache seems to be appending / to the base URL, I do not want this behavior. When I enable DirectorySlash Off it then tries to do a directory listing, resulting in a 403. All I want to do is keep the web server from 301'ing with a trailing / -- any help is appreciated. – emmdee Jul 8 '20 at 20:21
  • @emmdee If you have a question, please post it as a separate question rather than as a comment. – 200_success Jul 8 '20 at 20:26

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.