How to Set canonical for all PDF Files in subdirectory?

krlabs

New Member
#1
I have direcotry /books/ and i want put Canonical header for every file:

Code:
HTTP/1.1 200 OK

Content-Length: 19

...

Link: <https://www.example.com/downloads/white-paper.pdf>; rel="canonical"

...
Trying dynamic variables in VHost -> Context -> Static -> Header → I attempted using $0, $URI, $REQ_URI, and %{REQUEST_URI}, but none of them worked. For example:
Link: <https://kr-labs.com.ua/books/$0>; rel="canonical"

Also:

Code:
RewriteCond %{REQUEST_URI} ^/books/(.*\.pdf)$ [NC]
RewriteRule .* - [E=CANONICAL_URL:https://kr-labs.com.ua/books/%1]
Header set Link "<%{CANONICAL_URL}e>; rel=\"canonical\""

Can somethig help me or give some recommendations / tips?
 

LiteCache

Active Member
#2
Try this. Not tested, but should work:


Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_URI} \.pdf$ [NC]
    RewriteRule .* - [E=CANONICAL_URL:%{REQUEST_SCHEME}://%{HTTP_HOST}%{REQUEST_URI}]
</IfModule>

<IfModule mod_headers.c>
    Header set Link "<%{CANONICAL_URL}e>; rel=\"canonical\"" env=CANONICAL_URL
</IfModule>
 

krlabs

New Member
#3
Try this. Not tested, but should work:


Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteCond %{REQUEST_URI} \.pdf$ [NC]
    RewriteRule .* - [E=CANONICAL_URL:%{REQUEST_SCHEME}://%{HTTP_HOST}%{REQUEST_URI}]
</IfModule>

<IfModule mod_headers.c>
    Header set Link "<%{CANONICAL_URL}e>; rel=\"canonical\"" env=CANONICAL_URL
</IfModule>
Dont works. Tried in Context, in Rewrite tab and in .htaccess. Nothing.

For this moment i have only 1 solution, add in Static -> Context for /books/ directory in Headers section:

Link: <https://kr-labs.com.ua/books/>; rel="canonical"
 

krlabs

New Member
#5
Why do you need canonical URLs for PDF documents? Google is smart enough to recognize PDF documents.
For something PDF, damn Google put up the canonical URL from other sites on its own. Then, I read their documentation and they advise to specify the canonical URL in such situations.

This is important for me for indexing and getting files into search engines.
 

LiteCache

Active Member
#6
For something PDF, damn Google put up the canonical URL from other sites on its own. Then, I read their documentation and they advise to specify the canonical URL in such situations.

This is important for me for indexing and getting files into search engines.
I am sorry that a solution for this issue can only be provided by commercial soluton, but this forum doesn't allow it.
 

krlabs

New Member
#7
I am sorry that a solution for this issue can only be provided by commercial soluton, but this forum doesn't allow it.
What does “commercial” mean? This is the Open Source Forum. People who work with LiteSpeed Server on their own, without anyone else's support, gather here. This is exactly what I have been doing since 2020, when I registered here. And I solve all my cases myself by 90%.
 
#8
It seems you have a wrong understanding what OpenSource means. A provider like OpenLitespeed can offer free sources of the OLS, but that doesn't mean that any support is free. OpenLiteSpeed provides support for free, but if OLS doesn't provide a solution for your custom need and a external provider offers a solution for your need this service can, but must not be free. You can get OLS source code free, but if you have a custom need you must pay for it if the official OLS support provide any solution.

Just simply, isn't it?
 
Top