Toll Free: 1-877-695-7388

GTA: (647) 699-2838

Search Engine People
  • SEO
  • SEM
  • CRO
  • Display
  • Blog
  • Why Us
  • Contact
  • Join Our Team
  • Get A Quote

Toll Free: 1-877-695-7388

GTA: (647) 699-2838

How To Implement rel="canonical" HTTP Headers Using The .htaccess File

Alex Harvey | August 17th, 2011
Tweet25
Share1
Share
Pin
26 Shares

In a previous post I outlined how to implement rel=canonical HTTP headers using ASP.NET C#, ASP VBScript and PHP. However, there is a much simpler way to do this via the .htaccess file. Before we get into the code, lets recap on why you might want to use, and in what situations you could use an http header canonical link.

Back in June Google announced that its web search now supports a rel=canonical link relationship in HTTP headers. This is useful for both SEOs and webmasters who are combating the likes of PDF documents ranking higher than their preferred HTML pages. If your company publishes documents in both HTML format and PDF format, then using the HTTP header canonical link will help you to tell Google which is the preferred or main format that it should rank. This is even more essential if you are using Analytics to track traffic to your website as PDF downloads arent tracked in Analytics. Potentially youre missing out on traffic and accurate Analytics metrics if your PDF documents rank higher than your HTML pages.

For many people, HTTP headers are an area of development that is all too often overlooked. Take for example a 404 page; do you check to see if your 404 page serves the correct HTTP header response? Its important to know how to check response headers and what they mean, but thats for a different post. Heres what Googles 404 page response header looks like, which is correct:

404 Page Response Headers

The Code

By far the easiest way to implement an HTTP header canonical link is via the .htaccess file. This is because the file can be accessed via FTP and the majority of developers know what they are doing with a .htaccess file. If you are unsure of what you are doing I suggest you stay clear of the .htaccess file as you can quite easily make a mess of things.

In the following example we going to use the .htaccess file to place an HTTP header canonical link from a PDF file called white-paper.pdf to an HTML file called white-paper.html. In this case both files are placed on the root of the domain:

<FilesMatch "white-paper.pdf">

Header set Link '< http://yoursite.com/white-paper.html>;rel="canonical"'

</FilesMatch>

To test that everything is working correctly, use a plugin such as Live HTTP Headers for Firefox. If you need to implement a large number canonical links than your best bet is to use the methods in my previous post as a .htaccess file can become very bloated using this method. Its important to gain control and manage all of the files on your website and using rel=canonical HTTP headers is a great way of achieving this.

Tweet25
Share1
Share
Pin
26 Shares
Posted in Web DesignTagged Development, howto, onsite, SEO, Web Development

About the Author: Alex Harvey

Alex works for Fresh Egg, an SEO and multi discipline search agency. He has an extensive background as a web developer.

SEO & Web Design Blog

One thought on “How To Implement rel="canonical" HTTP Headers Using The .htaccess File”

  1. Joydeep says:
    August 19, 2011 at 12:43 am

    Thanks Alex, really helpful and useful post.

    One more question, can I use absolute URL or relative URL is a must?

    FilesMatch “http://www.example.com/pdf/white-paper.pdf”

Comments are closed.

Recent Posts

  • The Manifest Names Search Engine People Among Toronto’s Most Reviewed SEO Companies
  • Movin’ On Up! Why Migrating to Google Analytics 4 (GA4) Should be a Priority
  • A Year in Review: The Digital Marketing Trends That Defined 2021
  • The Basics of Video Marketing
  • Just How Much Do Google Reviews Impact Your SEO Ranking?

Categories

  • Analytics & ROI Analysis
  • Company News
  • Content
  • Conversion Optimization
  • Display Advertising/RTB
  • Email Marketing
  • En Español
  • En Français
  • Inbound Marketing
  • Lead Nurture & Marketing Automation
  • Local Search
  • Marketing
  • Mobile
  • Partnership Marketing
  • PPC
  • PR
  • SEO
  • Social Media Marketing
  • Web Design

Additional Posts

Drowning in PPC Data? Learn to S.W.I.M.

August 16th, 2011 | by Robert Brady

The 4 Types Of Inline Links (& How Not To Be A Douche With Them)

August 16th, 2011 | by Ruud Hein
Website Server Outages and Downtime

Best Practices for handling Website Server Outages and Downtime

August 15th, 2011 | by Joydeep Deb

LET'S TALK

Need more information or want to get in touch?

Get in touch!
  • SEO
  • SEM
  • Display
  • Blog
  • Why Us
  • Join Our Team
  • Contact Us
  • Local SEO
  • Small Business SEO
  • Enterprise SEO
  • International SEO

LOCATION

1305 Pickering Parkway,
5th Floor Pickering, L1V 3P2

PHONE

Toll Free: 1-877-695-7388
Greater Toronto Area: (647) 699-2838

Social

© Search Engine People Inc. 2023 – Canada’s Top Digital Agency
© SEP 2023 – A Search Engine People Company | Privacy Policy

Search Engine People