Canonical a pdf

I have tried elsewhere to find this information but without success. Can anyone tell me how to apply individual canonicalisation to several pdfs on a web page?
Someone suggested this code.
Here’s an approach you can take:
1 Capture the Request URI: Use mod_rewrite to capture the request URI of the PDF file.
2 Set Environment Variable: Store the captured URI in an environment variable.
3 Modify the Header: Use this variable to dynamically generate the canonical link.
Here is the example code:
<FilesMatch “.(pdf)$”>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*).pdf$
RewriteRule . - [E=PDF_REQUEST_URI:%1]

Header set Link '<https://mysite/%{PDF_REQUEST_URI}e>; rel="canonical"'

BUT I have no idea how to use this code.

Hi @paulcruice

Place this code in the .htaccess file at the root of your site to set a canonical link header for each PDF file. I adjusted the code (small syntax issue). You need to replace ‘mysite’ with your own domain. Hope that helps.

<FilesMatch “.pdf$”>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*)\.pdf$
RewriteRule .* - [E=PDF_REQUEST_URI:%1]
Header set Link “https://mysite/%{PDF_REQUEST_URI}e; rel="canonical"”

1 Like

Bruno,
Thanks for the quick reply.
I presume I still need to add a closing statement???

I have noticed a few things changed from the code I gave you (see my code below and your changes to it) compared to the code you sent me in your email.

<FilesMatch ".(pdf)$”> (you removed the \ and the ( ) brackets)

RewriteEngine On

RewriteCond %{REQUEST_URI} ^/(.*).pdf$ (you removed the * and inserted the )

RewriteRule . - [E=PDF_REQUEST_URI:%1]

Header set Link 'https://mysite/%{PDF_REQUEST_URI}e; rel="canonical”’ (Both single inverted commas marked in red, you have changed to double inverted commas and you have removed the >)

I presume all these are the syntax errors you mentioned.
Can you please confirm this for me.?

Cheers,
Paul

.

I should have mentioned that you removed both < and > in the Header link statement.
Paul

I’m sorry for this problem, I can’t get the forum’s text editor to display the special characters as I want, I sometimes have to precede them with a special character to prevent them from changing or not appearing (Markdown or not Markdown… blablablah). I hate this editor that didn’t plan for us to be able to exchange pieces of code…
Can you send me your email address by DM so that I can send you the correctly written bit of (with a decent text editor).
Oh yes in case Dan reads our conversation: Hence the importance of the text editor in Elements!!! :crazy_face:

@Bruno format the text using "code </>" button above (the one with the angle brackets and the backslash). Then everything you put in-between the back-ticks should show as expected. The syntax colouring will likely not be correct, but all of the intended text should be there.

For example:

<FilesMatch ".(pdf)$”>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(.*).pdf$
RewriteRule . - [E=PDF_REQUEST_URI:%1]
Header set Link '<https://mysite/%{PDF_REQUEST_URI}e>; rel="canonical"'
1 Like

hhhaaaaa so that’s it… ok Dan: please consider adding a very very detailed manual for the Elements text editor. :rofl:
@paulcruice I try now, here the code to put in the htaccess file :

<FilesMatch "\.pdf$">
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/(.*)\.pdf$
    RewriteRule .* - [E=PDF_REQUEST_URI:%1]
    Header set Link "<https://mysite/%{PDF_REQUEST_URI}e>; rel=\"canonical\""
</FilesMatch>

Don’t forget to replace “mysite” by your domain.
Hope that helps.

1 Like

Bruno, don’t understand what you mean by DM. You can contact me here. papavcharlieatgmaildotcom
Does that help? Otherwise explain what you mean by DM.

Cheers,
Paul Cruice

OK Bruno.
This time my server does not give an error message.
How can I test it is working?
Cheers,
Paul
papavcharlie@gmail.com

Hi @paulcruice

A DM is a direct message (right click on my icon and then click on the envelope icon ; sorry not to have precised it to you).

Here’s a Chatgpt answer to your question (I’m on my IPhone now so I chose the easy way to write :face_with_hand_over_mouth:) :
Here’s how to use an online header checker to verify that your .htaccess code is correctly adding the canonical header to PDF files:

  1. Choose an Online Header Checker Tool

There are several free online tools you can use to check the HTTP headers of a URL. Here are a few options:

httpstatus.io

webconfs.com HTTP Header Checker

tools.keycdn.com HTTP Header Checker

  1. Enter the URL of Your PDF File

In the tool’s URL field, enter the full URL of one of your PDF files (e.g., Free classifieds - yourdomain.com).

  1. Run the Check

Click the button to check or analyze the URL. The tool will send an HTTP request to your server and display the response headers it receives.

  1. Look for the Canonical Header

In the results, look for a line that starts with Link:. It should look like this:

Link: https://yourdomain.com/sample.pdf; rel=“canonical”

This confirms that the canonical header has been added to the PDF file.

  1. Test Multiple PDFs

If you have several PDF files, repeat the process for each one to ensure that the .htaccess code is correctly applying the canonical header to all PDF files on your site.

By following these steps, you can confirm that the canonical header is properly configured for each PDF file on your site.
Alternatively, you can use the developer console in a browser like Safari or Firefox to check the headers:

  1. Open the PDF URL in Your Browser

Go to the URL of one of your PDF files (e.g., Free classifieds - yourdomain.com).

  1. Open the Developer Console

• In Safari: Enable the “Develop” menu if it’s not already active (Safari > Preferences > Advanced > “Show Develop menu in menu bar”). Then, open the Develop menu and select “Show Web Inspector.”

• In Firefox: Right-click on the page and select “Inspect Element” or press F12 to open the Developer Tools.

  1. Go to the Network Tab

In the Developer Tools panel, navigate to the “Network” tab. This will show all the network requests the browser makes.

  1. Refresh the Page

Reload the PDF file’s URL to ensure it appears in the Network list.

  1. Select the PDF Request and View Headers

Click on the request for the PDF file. Under the “Headers” section, you’ll see all HTTP headers for that request, including the Link header if it’s correctly set.

  1. Check for the Canonical Header

Look for a Link header with the canonical URL in the format:

Link: https://yourdomain.com/sample.pdf; rel=“canonical”

This approach allows you to check headers directly in your browser without any external tools.

Hope that helps.

Bruno,
It works brilliantly!
How can I thank you enough!
I have spoken to a dozen or more from Google Search Console Community and some others and NONE of them have been able to help me.
I even tried ChatGTP but it didn’t get it right either. Thank you, thank you thank you, so much.

BUT Bruno, I have another problem to do with the .htaccess file which even my hosting company< Crazy Domains, can’t get right.
Are you prepared to have a go at this too?
If you can’t already see it my private email is paulcruice@gmail.com
Would love to speak to you directly if you don’t mind.
Cheers,
Paul

1 Like

Glad to have helped. I’ve just sent you an email.