.htaccess中的PDF和图像文件的规范标题链接 [英] Canonical Header Links for PDF and Image files in .htaccess

查看:105
本文介绍了.htaccess中的PDF和图像文件的规范标题链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的网站上的许多PDF和图像文件设置规范链接。

I'm attempting to setup Canonical links for a number of PDF and images files on my website.

示例文件夹结构:

/index.php
/docs/
    file.pdf
    /folder1/
        file.pdf
    /folder2/
        file1.pdf
        file2.pdf
/img/
    sprite.png
    /slideshow/
        slide1.jpg
        slide2.jpg

示例URL到规范URL:
http://www.example.com/docs/folder1/file.pdf-> http://www.example.com/products/folder1/

Example PDF URL to Canonical URL: http://www.example.com/docs/folder1/file.pdf --> http://www.example.com/products/folder1/

我试图避免在每个文件中都放置单独的.htaccess文件包含我所有图像和PDF的子文件夹。我目前有7个主文件夹,并且这些文件夹中的每个都有2-10个子文件夹,大多数子文件夹都有自己的子文件夹。我大约有80个PDF,甚至还有更多图像。

I am trying to avoid having to put individual .htaccess files in each of the sub-folders that contain all of my images and PDFs. I currently have 7 "main" folders, and each of these folders have any where from 2-10 sub-folders, and most sub-folders have their own sub-folders. I have roughly 80 PDFs, and even more images.

我正在寻找一种(半)动态解决方案,其中某个文件夹中的所有文件都将设置Canonical Link到单个网址。我想尽可能地将其保存在单个.htaccess文件中。

I'm looking for a (semi)dynamic solution where all files in a certain folder will have the Canonical Link set to a single url. I want to keep as much as possible in a single .htaccess file.

我知道< Files> < FilesMatch> 不理解路径,并且< Directory> < ; DirectoryMatch> 不能在.htaccess文件中使用。

I know that <Files> and <FilesMatch> do not understand paths, and that <Directory> and <DirectoryMatch> don't work in .htaccess files.

是否有一种相当简单的方法来完成此操作?

Is there a fairly simple way to accomplish this?

推荐答案

我不知道一种单独使用apache规则解决此问题的方法,因为它需要某种正则表达式匹配并重用匹配结果

I don't know of a way to solve this with apache rules alone as it would require some sort of regex matching and reusing the result of the match in a directive, which isn't possible.

但是,如果将php脚本引入组合中,则非常简单:

However, it's pretty simple if you introduce a php script into the mix:

RewriteEngine On
RewriteCond %{REQUEST_URI} \.(jpg|png|pdf)$
RewriteRule (.*) /canonical-header.php?path=$1

请注意,这会将所有jpg,png和pdf文件的请求发送到脚本,无论文件夹名称如何。如果只想包含特定的文件夹,则可以添加另一个RewriteCond来完成。

Note that this would send requests for all jpg, png and pdf files to the script regardless of the folder name. If you want to include only specific folders, you could add another RewriteCond to accomplish that.

现在可以使用canonical-header.php脚本:

Now the canonical-header.php script:

<?php

// Checking for the presence of the path variable in the query string allows us to easily 404 any requests that
// come directly to this script, just to be safe.
if (!empty($_GET['path'])) {
    // Be sure to add any new file types you want to handle here so the correct content-type header will be sent.
    $mimeTypes = array(
        'pdf' => 'application/pdf',
        'jpg' => 'image/jpeg',
        'png' => 'image/png',
    );

    $path         = filter_input(INPUT_GET, 'path', FILTER_SANITIZE_URL);
    $file         = realpath($path);
    $extension    = pathinfo($path, PATHINFO_EXTENSION);
    $canonicalUrl = 'http://' . $_SERVER['HTTP_HOST'] . '/' . dirname($path);
    $type         = $mimeTypes[$extension];

    // Verify that the file exists and is readable, or send 404
    if (is_readable($file)) {
        header('Content-Type: ' . $type);
        header('Link <' . $canonicalUrl . '>; rel="canonical"');
        readfile(realpath($path));
    } else {
        header('HTTP/1.0 404 Not Found');
        echo "File not found";
    }
} else {
    header('HTTP/1.0 404 Not Found');
    echo "File not found";
}

请考虑此代码未经测试,并在发布之前检查其是否在所有浏览器中都能正常工作投入生产。

Please consider this code untested and check that it works as expected across browsers before releasing it to production.

这篇关于.htaccess中的PDF和图像文件的规范标题链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆