URL的相對路徑問題

對於url的相對路徑問題一直都沒太仔細弄清楚,這次好好的查了一下:

rfc1630中有這樣一段話:

The rules for the use of a partial name relative to the URI of the context are:

 

If the scheme parts are different, the whole absolute URI must be given.  Otherwise, the scheme is omitted, and:

 

If the partial URI starts with a non-zero number of consecutive slashes, then everything from the context URI up to (but not including) the first occurrence of exactly the same number of consecutive slashes which has no greater number of consecutive slashes anywhere to the right of it is taken to be the same and so prepended to the partial URL to form the full URL. Otherwise:

 

The last part of the path of the context URI (anything following the rightmost slash) is removed, and the given partial URI appended in its place, and then:

 

Within the result, all occurrences of "xxx/../" or "/." [這裏大概是”./”]Are recursively removed, where xxx, ".." and "." are complete path elements.

 

URL可以看做是URI的一個子集,上面的意思放在URL的範疇上可以做如下解釋:

1. 就是如果想用相對路徑來表示URL,則該URL與其父URL應該是同一種方法(例如:都是http,或都是ftp),否則需要用絕對路徑來表示。

 

2. 如果相對路徑表示的URL是以反斜槓開頭的,[這段的英文有點繞,看的不是太清楚]則取其父URL第一個相同數量反斜槓位置的左側部分[連續多個反斜槓?貌似http裏面很少看到],加上相對路徑表示URL,組成完整的URL

例如:頁面中的有一個URL /info/test.html ,它的父鏈是 http://www.example.com/index.html,則這個相對URL的完整鏈接是:http://www.example.com/info/test.html

 

3. 如果相對路徑表示的URL不是以反斜槓開頭,則移除該URL父鏈的最後一個部分,拼接上該相對路徑表示的URL,組成完整的URL

例如:頁面中有一個URL test.html ,父鏈是http://www.example.com/info/index.html,則完整鏈接是http://www.example.com/info/test.html

另外如果URL ../test.html,父鏈是http://www.example.com/info/index.html,則完整鏈接是http://www.example.com/test.html。這個和UNIX文件系統是的表示是一樣的。

好了 除此之外,還需要注意另外一種情況:

如果html頭部 <base>標記,例如:<base href="http://www.example.com/info/" />則該頁面上的基準URL都以此爲準,不考慮其父鏈的情況。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章