redirect browser in SimpleHTTPServer.py?

2019-07-22 20:32发布

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?

2条回答
▲ chillily
2楼-- · 2019-07-22 20:45

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.

查看更多
爷、活的狠高调
3楼-- · 2019-07-22 20:49

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.

查看更多
登录 后发表回答