4

当我有一个像http://site.com这样的链接时,点击它就可以转到那个 URL。

但是当链接只是 时www.site.com,它会将此 URL 添加到父网站,因此不会转到该 URL。

因此,单击<a href='www.site.com'>site</a>会在浏览器任务栏中创建:www.parentsite.com/www.site.com.

如何在不使用的情况下解决此问题preg_replace?我对这个游戏相当陌生。

4

1 回答 1

7

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.

于 2012-11-07T18:41:01.853 回答