Search engines will complain if websites and files do not have a canonical name, and appear under multiple different URLs with the same content. Especially for search engine optimization this is a big no no.
How does that happen
Someone might link to your resource with a tracking link, as example this entry in the webserver log:
xyz.xyz.xyz.xyz - - [20/Dec/2024:01:04:57 +0100] "GET /writings/Add_Geocoordinates_to_your_addresses_in_PostgreSQL_-_Meetup_HH_April_2024.pdf?fbclid=<some random id> HTTP/1.1" 200 1032842 "-" "Mozilla/<some random browser>"
The attached fbclid header makes it obvious that this PDF was posted on Facebook, and then Facebook attaches a semi-random string, it’s called the Facebook Click Identifier. It is used to circumvent Ad-blockers and Third-Party-Cookie Protection and allows Facebook to track the performance of the link.
It also creates two unique URLs for the same content, at least. One without fbclid, one with every single fbclid attached.
Add to that problem the gclid Google Click Identifier and the li_fat_id LinkedIn Click Identifier and it becomes obvious how this is a problem for SEO.
A search engine might stumble over any of those links, read the content of the link, receive the PDF and identify it as “the same content as the other URLs”.
How to prevent this
The HTML5 standard knows the link rel="canonical" header with the canonical path for a resource. Every time a search engine fetches the website, it uses this header as original path and can apply the content of the path with tracking id to the canonical path, as defined by the website creator.
As example:
| |
This is the canonical URL for this blog posting.
But that does not work with files. It’s not possible to add a HTML5 header to a PDF. However there is a second way: add the canonical name as HTTP header.
This is easy to do in Apache2, using the mod_rewrite and mod_headers modules.
| |
The piece of config above will store the Server Name and the Request URI (the path, without parameters) in environment variables RU and SN, and then add the Link header with the canonical name.
The following example uses curl to fetch a PDF from the website with a parameter attached:
| |
Along with the content of the file (Content-Length: 1032842), the webserver also returns the canonical name:
Link: <https://andreas.scherbaum.la/writings/Add_Geocoordinates_to_your_addresses_in_PostgreSQL_-_Meetup_HH_April_2024.pdf>; rel="canonical"
That’s neat!
Other options
One possible other option to solve this problem is to 301 (permanent) redirect every request with a parameter to the canonical URL. However this approach is problematic: many websites use parameters for valid website operations, think CMS. This only works if you are sure not to destroy the link while removing parameters.
It also doesn’t work well with POST request. However in the example above a file will never receive valid POST requests, therefore I don’t need to handle this case.
Comments
With an account on the Fediverse or Mastodon, you can respond to this post. Known replies are displayed below:
Note: This will load data from mastodon.social.
Learn how this is implemented here.
Share: