ww...">

Href without http (s) prefix

I just created a primitive html page. Here it is: an example And here is its markup:

<a href="www.google.com">www.google.com</a>
<br/>
<a href="http://www.google.com">http://www.google.com</a>

      

As you can see, it contains two links. The first href has no "http" prefix, and when I click that link, the browser redirects me to a non-existent page https://fiddle.jshell.net/_display/www.google.com

. The second href has this prefix and the browser produces the correct URL http://www.google.com/

. Is it possible to use hrefs such as www.something.com

without the http (s) prefixes?

+3


source to share


2 answers


It is possible, and indeed, you are doing it right now. It just doesn't do what you think it does.

Check out what the browser does when you link to this:

href="index.html"

      

What then would you do when you refer to this?

href="index.com"

      

Or that?:

href="www.html"

      



Or:

href="www.index.com.html"

      

The browser doesn't know what you mean, it only knows what you told it. Without the prefix, it will follow the standard for the current HTTP address. The prefix is ​​what tells him to start at the whole new root address.

Note that you don't need the part http:

, you can do this:

href="//www.google.com"

      

The browser will use what the current protocol is ( http

, https

etc.), but will //

report that this is the new root address.

+3


source


You can omit the protocol by using //

before this path. Here's an example:

<a href="//www.google.com">Google</a>

      



By using //

, you can tell the browser that this is actually a new (complete) link, not a relative one (relative to your current link).

+2


source







All Articles