Search code examples
htmlhref

How to make HTML href function to redirect the user to a world wide web page instead of a folder inside the user's computer?


I am learning HTML, and whenever I execute the href function in HTML and click the blue text, the browser tries to redirect me to a folder inside my computer, when in reality I want to enter a website. For example, if I try to execute the following code, instead of the browser redirecting me to duckduckgo.com, it tries to redirect me to a folder inside my computer:

<a href="duckduckgo.com">Browse anonymously and without being traced</a>

How can I solve this issue?


Solution

  • Because href="duckduckgo.com" is using a relative URL, so the browser is looking for duckduckgo.com relative to the current URL that is displaying the page. To the browser it's no different than if you used href="index.html", both are structurally identical.

    Instead, use a fully-qualified URL:

    <a href="http://duckduckgo.com">Browse anonymously and without being traced</a>
    

    You can also default to the current protocol with this:

    <a href="//duckduckgo.com">Browse anonymously and without being traced</a>
    

    So if the current page is open via http:// or https:// then the link would use the same in the resulting request. Note however that your description of "a folder inside my computer" may somewhat imply that your current protocol could be file://, in which case an inferred protocol clearly wouldn't work. The point is, the structure of a complete URL is pretty versatile so you have options.