Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Href without http(s) prefix

Tags:

html

href

I just have created primitive html page. Here it is: 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 one's href doesn't have 'http'-prefix and when I click this link browser redirects me to non-existing page https://fiddle.jshell.net/_display/www.google.com. The second one's href has this prefix and browser produces correct url http://www.google.com/. Is it possible to use hrefs such as www.something.com, without http(s) prefixes?

like image 836
Pupkin Avatar asked May 05 '17 11:05

Pupkin


People also ask

Do you need https in href?

Answer. Links are recommended to be inserted with the https or http protocol. Certain URLs, for example subdomain1.subdomain2.domain.com are saved as a relative path, if the protocol was not added. Adding the https or http protocol prevents links from being as relative paths, or anchor links in the body of the article.

Do all links have HTTP?

URLs in href are not restricted to only HTTP documents. They support all the protocols supported by browsers- ftp, mailto, file etc.

What is href =# in HTML?

Definition and Usage. The href attribute specifies the URL of the page the link goes to. If the href attribute is not present, the <a> tag will not be a hyperlink. Tip: You can use href="#top" or href="#" to link to the top of the current page!

Is href attribute mandatory?

The tag is fine to use without an href attribute. Contrary to many of the answers here, there are actually standard reasons for creating an anchor when there is no href. Semantically, "a" means an anchor or a link. If you use it for anything following that meaning, then you are fine.


2 Answers

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

Consider what the browser does when you link to this:

href="index.html" 

What then would it do when you link to this?:

href="index.com" 

Or this?:

href="www.html" 

Or?:

href="www.index.com.html" 

The browser doesn't know what you meant, it only knows what you told it. Without the prefix, it's going to follow the standard for the current HTTP address. The prefix is what tells it that it needs to start at a new root address entirely.

Note that you don't need the http: part, you can do this:

href="//www.google.com" 

The browser will use whatever the current protocol is (http, https, etc.) but the // tells it that this is a new root address.

like image 192
David Avatar answered Sep 22 '22 02:09

David


You can omit the protocol by using // in front of the path. Here is an example:

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

By using //, you can tell the browser that this is actually a new (full) link, and not a relative one (relative to your current link).

like image 20
ssc-hrep3 Avatar answered Sep 21 '22 02:09

ssc-hrep3