Sub topic 2
The parts of a URL
This chapter assumes you know basic HTML, including the a element and its href attribute. It also assumes you know that a browser requests a page over HTTP. Here you learn to split a URL into its parts and to see which parts search engines treat as part of a page's identity. The next chapters build on this when they cover slugs and duplicate URLs.
Why the parts matter
A search engine stores pages by URL. Two URLs that differ by one character can be two separate pages in its index, even when the content is identical. To control which URL represents a page, you need to know what each piece of the URL does.
Anatomy of a URL
Take this address:
https://www.example.com:443/blog/url-slugs/?utm_source=news&page=2#examples
| Part | Example | What it does |
|---|---|---|
| Scheme | `https` | The protocol used to fetch the page. |
| Host | `www.example.com` | The server name. The subdomain (`www`) is part of the host. |
| Port | `443` | The network port. It is usually omitted because `https` defaults to 443. |
| Path | `/blog/url-slugs/` | Which resource on the host to return. |
| Query string | `?utm_source=news&page=2` | Name and value pairs sent to the server. |
| Fragment | `#examples` | A position inside the page, handled by the browser. |
The last segment of the path, url-slugs, is the slug. The next chapter covers slugs in detail. Here it is enough to know that the slug sits inside the path.
Splitting a URL in code
Python's standard library can separate these parts, which is useful when you audit many URLs later.
from urllib.parse import urlsplit, parse_qs
url = "https://www.example.com:443/blog/url-slugs/?utm_source=news&page=2#examples"
parts = urlsplit(url)
print(parts.scheme) # https
print(parts.hostname) # www.example.com
print(parts.port) # 443
print(parts.path) # /blog/url-slugs/
print(parts.query) # utm_source=news&page=2
print(parts.fragment) # examples
print(parse_qs(parts.query)) # {'utm_source': ['news'], 'page': ['2']}Which parts change page identity
Search engines generally treat a different value in any of these parts as a different URL:
- Scheme.
http://andhttps://versions of a page are separate URLs. - Host.
example.comandwww.example.comare separate hosts, so they are separate URLs. - Path. Paths are case-sensitive on most servers.
/Blog/and/blog/can be two pages. A trailing slash can also create a second URL (/blogand/blog/). - Query string. Each parameter combination is a distinct URL. Tracking parameters such as
utm_sourceoften create many URLs for one page.
The fragment is different:
Common errors
- Mixing versions in internal links. If some links point to
http://or to the non-wwwhost, crawlers meet several versions of the same page. Pick one scheme and host and use it everywhere. - Letting parameters leak into links. Linking internally to
?utm_source=...URLs spreads signals across duplicates and makes analytics unreliable. Reserve tracking parameters for external campaigns. - Mixed-case paths. Use lowercase paths so that one page has one address.
- Inconsistent trailing slashes. Choose a convention and redirect the other form to it.
Fixing these problems relies on redirects and canonical tags. Later chapters cover both. For now, the goal is to look at a URL and name which part makes it differ from another.
Exercise
A site serves the same article at https://example.com/Guide/, https://example.com/guide/, and https://example.com/guide/#faq. How many distinct URLs does a search engine most likely see, and which part causes each difference?
Show answer
Two. /Guide/ and /guide/ differ in the path because paths are usually case-sensitive. The third address, /guide/#faq, differs only in the fragment, which search engines ignore, so it counts as the same URL as /guide/.