tags:

views:

1803

answers:

3

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

And your server configuration automatically redirects this request to

www.example.com/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?

+4  A: 

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 command to the browser and the browser requests the new url then the browser does know about it and you can get it.

Vincent Ramdhanie
+1  A: 

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

Paul Fisher
+4  A: 

The problem is that it's not actually a redirect. When you type 'www.example.com' 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.com
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.

Adam Rosenfield