Sub topic 1
Anatomy of a hyperlink: href, anchor text, and rel
Every backlink is built from an ordinary HTML link. To judge a backlink later in this tutorial, you first need to read one precisely. This chapter takes a link apart into its three parts: the target in href, the anchor text, and the rel attribute.
Prerequisites
This chapter assumes you can do the following:
- Explain what a website, a web page, and a URL are.
- Read simple HTML, including tags and attributes.
- Run a short Python script, if you want to try the code example.
- Describe in general terms how search engines crawl and index pages.
The anchor element
A hyperlink in HTML is written with the anchor element, <a>. It has three parts that matter for search:
hrefis an attribute holding the URL the link points to. Withouthref, the element is not a link to anything.- Anchor text is the visible text between the opening and closing tags. Browsers usually show it underlined or colored.
relis an optional attribute describing the relationship between the linking page and the target.
<a href="https://example.com/guide" rel="nofollow">Example guide</a>Here https://example.com/guide is the target, "Example guide" is the anchor text, and nofollow is the rel value. The nofollow value hints that the linking site does not vouch for the target.
Direction matters
A link has two ends. Seen from the page that contains it, it is an outgoing link. Seen from the page named in href, the same link is a backlink. The HTML is identical in both cases. Only your point of view changes. The next chapter covers backlinks in detail. For now, remember that whoever writes the <a> tag controls the anchor text and the rel value, and the target page does not.
Reading href carefully
The value of href can take several forms:
- An absolute URL such as
https://example.com/guidenames the full address, including the domain. - A relative URL such as
/guideor../guideis resolved against the current page. Relative links usually stay on the same site. - A fragment such as
#pricingpoints to a spot within a page. - Schemes such as
mailto:orjavascript:do not point to a web page, so they do not behave like links between pages.
A crawler normally resolves relative URLs into absolute ones before recording them. When you inspect links, always compare the resolved URL, not the raw text.
Anchor text
Anchor text is the clickable words. Search engines read it as a short description of the target page. "SEO guide" tells a reader and a crawler what to expect. "Click here" tells them nothing. Anchor text can also be empty or consist of an image, in which case search engines generally use the image's alt text as the anchor text. Anchor text gets its own chapter later.
The rel attribute
The rel attribute can hold one or more space-separated values. Three matter for backlinks:
| Value | Meaning |
|---|---|
| `nofollow` | The linking site does not vouch for the target. |
| `sponsored` | The link is a paid or compensated placement. |
| `ugc` | The link sits in user-generated content, such as a comment or forum post. |
A link with no rel value is a plain link, and the linking site is endorsing the target. Values can be combined:
<!-- Paid placement -->
<a href="https://example.com/product" rel="sponsored">Partner product</a>
<!-- Link in a user comment or forum post -->
<a href="https://example.com/post" rel="ugc">A commenter's link</a>
<!-- Several values can be combined -->
<a href="https://example.com/offer" rel="sponsored nofollow">Offer</a>The rules for choosing between these values, and how search engines treat them, come in a later chapter.
Extracting link facts with Python
A crawler records the same three facts for each link: the target, the rel values, and the anchor text. This script does that with Python's standard library.
from html.parser import HTMLParser
class LinkCollector(HTMLParser):
"""Collect href, rel values, and anchor text for every <a> tag."""
def __init__(self):
super().__init__()
self.links = []
self._current = None
def handle_starttag(self, tag, attrs):
if tag == "a":
attributes = dict(attrs)
if "href" in attributes:
self._current = {
"href": attributes["href"],
"rel": (attributes.get("rel") or "").split(),
"text": "",
}
def handle_data(self, data):
if self._current is not None:
self._current["text"] += data
def handle_endtag(self, tag):
if tag == "a" and self._current is not None:
self._current["text"] = self._current["text"].strip()
self.links.append(self._current)
self._current = None
html = '<p>Read the <a href="https://example.com/guide">SEO guide</a> and <a href="https://example.org/ad" rel="sponsored nofollow">this offer</a>.</p>'
parser = LinkCollector()
parser.feed(html)
for link in parser.links:
print(link)The output is two dictionaries:
{'href': 'https://example.com/guide', 'rel': [], 'text': 'SEO guide'}
{'href': 'https://example.org/ad', 'rel': ['sponsored', 'nofollow'], 'text': 'this offer'}The first link has an empty rel list, so it is a plain link. The second carries two values.
Common errors
- Ignoring
relwhen reading a link. Two links with the same target and anchor text can mean different things to a search engine because ofrel. - Treating
relas one string. It is a list of tokens. Check fornofollowby splitting on whitespace, not by comparing the whole string. - Judging a relative
hrefby its text./guidehas no domain, so resolve it against the page URL first. - Assuming a link tag is always visible. An
<a>element with an image or empty text still counts as a link.
Exercise
A page contains <a href="/pricing" rel="ugc nofollow">See plans</a>. Name the target, the anchor text, and the rel values, and say what the ugc value tells a search engine.
Show answer
The target is /pricing, which resolves against the current page's URL. The anchor text is "See plans". The rel values are ugc and nofollow. The ugc value tells a search engine that the link was placed in user-generated content, such as a comment or forum post, so the site owner did not necessarily choose it.