Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
833 views
in Technique[技术] by (71.8m points)

html - Absolute URL's acting like relative URL's

When I have a link like http://site.com, clicking it goes to that URL just fine.

But when the link is just www.site.com, it adds this URL to the parent website and consequently does not go to the URL.

So, clicking on <a href='www.site.com'>site</a> creates this in the browser taskbar: www.parentsite.com/www.site.com.

How can I fix this without using preg_replace? I am fairly new to this game.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because www.parentsite.com is interpreted as a relative address, like for example index.htm, as opposed to an absolute URL which consists of protocol, hostname, and path.

When I'm on example.com's front page....

  • contact.html is a relative address, the absolute end result will be http://example.com/contact.html (the browser does this as an internal calculation)

  • images/ is a relative address, resulting in http://example.com/images/

  • www.xyz.com results in http://example.com/www.xyz.com

You need to prefix the protocol (eg. http://) to make the browser understand that you mean a full URL, and treat it accordingly.

Here's a background info article on the issue on MSDN.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...