Friday, November 25, 2016

apache 2.2 - Apache2 - 301 Redirect when missing "/" at the end of directory in the url




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?


Answer



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 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.


No comments:

Post a Comment

linux - How to SSH to ec2 instance in VPC private subnet via NAT server

I have created a VPC in aws with a public subnet and a private subnet. The private subnet does not have direct access to external network. S...