redirect browser in SimpleHTTPServer.py?

2019-07-22 20:30发布

问题:

I am partially through implementing the functionality of SimpleHTTPServer.py in Scheme. I am having some good fun with HTTP request/response mechanism. While going through the above file, I came across this- " # redirect browser - doing basically what apache does" in the code".

Why is this redirection necessary in such a scenario?

回答1:

Imagine you serve a page

http://mydomain.com/bla

that contains

<a href="more.html">Read more...</a>

On click, the user's browser would retrieve http://mydomain.com/more.html. Had you instead served

http://mydomain.com/bla/

(with the same content), the browser would retrieve http://mydomain.com/bla/more.html. To avoid this ambiguity, the redirection appends a slash if the URL points to a directory.



回答2:

It simplifies things to treat the trailing / as irrelevant when the user does a GET on a directory, so that (say) http://www.foo.com/bar and http://www.foo.com/bar/ have exactly the same effect. Simplest (though not fastest, see Souders' books;-) is to have the former cause a redirect to the latter.