最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

dom - How do I get the URL path after a redirect -- with Javascript? - Stack Overflow

programmeradmin1浏览0评论

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 badges
Add a ment  | 

3 Answers 3

Reset to default 4

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

发布评论

评论列表(0)

  1. 暂无评论