This could be a little off the ballpark, but a friend asked me about it and it's kind of bothering me. How do you figure out the current path in your address bar if the server redirects all requests to a default file, say, index.html.
Let's say you entered:
www.example/
And your server configuration automatically redirects this request to
www.example/index.html
But the address in your url bar does not change! So how do you figure out using Javascript that the path on this url is index.html?
I looked into location.pathname but that's only giving me /.
Any ideas?
This could be a little off the ballpark, but a friend asked me about it and it's kind of bothering me. How do you figure out the current path in your address bar if the server redirects all requests to a default file, say, index.html.
Let's say you entered:
www.example./
And your server configuration automatically redirects this request to
www.example./index.html
But the address in your url bar does not change! So how do you figure out using Javascript that the path on this url is index.html?
I looked into location.pathname but that's only giving me /.
Any ideas?
Share Improve this question asked Dec 2, 2008 at 20:33 picardopicardo 24.9k33 gold badges108 silver badges156 bronze badges3 Answers
Reset to default 4The problem is that it's not actually a redirect. When you type 'www.example.'
into your web browser, the browser generates an HTTP request like the following:
GET / HTTP/1.1
User-Agent: curl/7.16.3 (i686-pc-cygwin) libcurl/7.16.3 OpenSSL/
0.9.8h zlib/1.2.3 libssh2/0.15-CVS
Host: www.example.
Accept: */*
Note that it's requesting a document named literally /
. There are two possible responses by the server:
HTTP/1.1 200 OK
(more headers)
Content...
And
HTTP/1.1 301 Moved Permanently
Location: (location of redirect)
(more headers)
(Optional content)
In Case #2, you can get the redirect location since it's actually a redirect. But in Case #1, the server is doing the redirection: it sees a request for /
and instead serves up the content for whatever its index page is (e.g. index.html
, index.php
, default.aspx
, etc.). As far as your browser is concerned, that's just the content for the document /
. It doesn't know it's been redirected.
If the redirect happens on the server then the browser is not aware of it and you cannot access the actual resource in javascript. For example, a servlet might "forward" to another resource on the server.
If the server sends a redirect mand to the browser and the browser requests the new url then the browser does know about it and you can get it.
There is no way to do this, except somehow embedding the webserver's filename into the document. As far as the browser is concerned, there is no index.html
, the page is just /
.