我在我的网站上有许多以数字结尾的pdf,尽管它们是相同的pdf。
我想通过.htaccess(使用Files或FilesMatch)将Canonical Link添加到以相应pdf的数字结尾的pdf
我认为模式必须是{anything} - {digit} .pdf
例如:
<Files my-pdf-name-ends-with-1.pdf>
Header add Link '<https://www.website.com/pdf/my-pdf-name-ends-with.pdf>; rel="canonical"'
</Files>
<Files my-pdf-name-ends-with-2.pdf>
Header add Link '<https://www.website.com/pdf/my-pdf-name-ends-with.pdf>; rel="canonical"'
</Files>
<Files newpdf45-that-ends-with-2.pdf>
Header add Link '<https://www.website.com/pdf/newpdf45-that-ends-with.pdf>; rel="canonical"'
</Files>
<Files newpdf45-that-ends-with-77.pdf>
Header add Link '<https://www.website.com/pdf/newpdf45-that-ends-with.pdf>; rel="canonical"'
</Files>
<Files other-pdf-name-that-ends-with-digit-45.pdf>
Header add Link '<https://www.website.com/pdf/other-pdf-name-that-ends-with-digit.pdf>; rel="canonical"'
</Files>
<Files other-pdf-name-that-ends-with-digit-34.pdf>
Header add Link '<https://www.website.com/pdf/other-pdf-name-that-ends-with-digit.pdf>; rel="canonical"'
</Files>
谢谢!
答案 0 :(得分:0)
要根据请求的文件名动态添加标题链接,您可以使用此
<FilesMatch "^(?<uripart[^0-9]+)[0-9]+\.pdf$">
Header add Link '<https://www.website.com/pdf/%{env:MATCH_URIPART}.pdf >;rel="canonical"'
</FilesMatch>
uripart
是我们从请求的pdf文件中捕获的uri字符串的一部分,我们使用ENV:MATCH
变量在标题目标中使用它。
答案 1 :(得分:0)
我的解决方案
<FilesMatch "^(?<uripart>[^0-9]+)(-)[0-9]+\.pdf$">
<If "%{REQUEST_URI} =~ m#^/path/to/pdfs/#">
RewriteRule .* - [E=FILENAME:https://%{HTTP_HOST}/path/to/pdfs/%{ENV:MATCH_URIPART}\.pdf]
Header set Link '<%{FILENAME}e>; rel="canonical"'
</If>
</FilesMatch>
<FilesMatch "^(?<uripart>[^0-9]+)\.pdf$">
<If "%{REQUEST_URI} =~ m#^/path/to/pdfs/#">
RewriteRule .* - [E=FILENAME:https://%{HTTP_HOST}%{REQUEST_URI}]
Header set Link '<%{FILENAME}e>; rel="canonical"'
</If>
</FilesMatch>