Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

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
( http://192.168.1.111/social )

Apache Server Responce:

HTTP/1.1 301 Moved Permanently
Location: http://192.168.1.111/social/

User Request:

GET /social/ HTTP/1.1
( http://192.168.1.111/social/ )

Apache Server Responce:

HTTP/1.1 200 OK

Apache access.log:

192.168.1.130 - - [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"
-
192.168.1.130 - - [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?

share|improve this question
    
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 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 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 at 9:49

2 Answers 2

up vote 7 down vote accepted

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.

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.