How to “Canonicalize” Images and PDFs with the .htaccess File

You may have heard about how adding a ‘rel=”canonical”‘ tag to the head of an HTML file can improve your SEO. And we all need to give our SEO a tune-up once in a while. This tag can be helpful if you have many versions of the same page, creating duplicate content (which is bad for SEO). Often, this can happen if the site’s code allows multiple URLs to generate the same page.

The ‘rel=”canonical”‘ tag helps your SEO by telling search engines what URL is the official (or, canonical) one. It’s like a pointer that refers the search engine to the canonical URL. And you can do this with your .htaccess file!

What about images and PDF files?

It’s true that static files like images and PDFs don’t have an HTML head area where you can put a canonical URL. Imagine that your site has thousands of images that have been indexed by Google. It’s possible someone might land on your image while searching.

Is there any SEO value in that? There can be, if you put a canonical URL for that image in the HTTP response headers. Follow along below and we’ll show you how to do that with your .htaccess file.

How to Canonicalize Images and PDFs

For the example below, let’s suggest that the image name has a corresponding HTML page of the same name. If anyone lands on that image, the HTTP response header will provide our canonical URL for the page associated with that image:

 Options +FollowSymlinks RewriteEngine On  RewriteRule ([^/]+)\.jpg$ - [E=FILENAME:$1]  <FilesMatch "\.jpg$">       Header add Link '<{FILENAME}e.html>; rel="canonical"' </FilesMatch>

You can use the same code for PDFs or other file types, just change the file extension provided.

You could also add this header to individual files:

 <Files image.jpg>       Header add Link '<>; rel="canonical"' </Files>

In the above, the URL is dynamically generated, and in the below, it’s hard-coded. You can use whichever way works best for your site.

How do you know it’s working?

You can test this procedure by opening your browser’s inspector or debugger and viewing the HTTP response headers. (How to do this will vary for different browsers.)

This code (slightly modified) can be placed in your Apache configuration, but that requires root access. For accounts that don’t have root, like our Shared Hosting accounts, the .htaccess file is the best place to do this.

InMotion Hosting Contributor
InMotion Hosting Contributor Content Writer

InMotion Hosting contributors are highly knowledgeable individuals who create relevant content on new trends and troubleshooting techniques to help you achieve your online goals!

More Articles by InMotion Hosting

4 thoughts on “How to “Canonicalize” Images and PDFs with the .htaccess File

  1. Thank you for the answer John. Is there anywhere specific in the .htaccess file I need to enter the piece of code or can I just enter it at the bottom of my .htaccess page.

    Thank you

  2. For the piece of code:

    Header add Link “; rel=\”canonical\””

    Where do I go to enter this in a non-html page. e.g Cpanel, File management, .htacess file etc
    Thank you

Was this article helpful? Join the conversation!

Web Hosting Cyber Week Sale
Hurry! These Deals Won't Last Long