Search code examples
htmlhyperlinkhref

<a href> requires http://www. or extends current URL


When I am using a <a href="examplesite.org" target="_blank"> if I click the link I am taken to currentsite/examplesite.org.

If I then change this to <a href="http://www.examplesite.org" target="_blank"> I am taken to the correct site.

As these links are data driven (pulled from a URLs table in my Database) I would rather not have to either:

  1. Hardcode 'http://www' into my html to get around it
  2. Include this in every url in my table, as some may not require it. (internal urls)

Any advice?


Solution

  • I suspect you could prepend // to each URL in your HTML:

    <a href="//examplesite.org" target="_blank">
    

    This will make the link relative to the current page’s protocol (i.e. http or https), but will otherwise make the href value be treated as a domain name.

    Of course, this assumes that the link’s href value will always be intended as a domain. It’s difficult to differentiate an href intended as a domain (e.g. examplesite.org) from one intended as a relative page link on your site (e.g. examplepage.html), especially now that top-level domains have gone insane.