apache - 如何即时更新 .htaccess 以有条件地 gzip

标签 apache .htaccess gzip brotli

注意事项

有人建议这是 How to serve precompressed gzip/brotli files with .htaccess 的副本.该问题仅寻求提供预压缩文件。这个问题是不同的。请看下面。

我的目标

我想在存在预压缩的 brotli 文件时提供它们。如果不存在预压缩的 brotli 文件,则回退到即时 gzip 压缩。

当前代码

我在一个网站上工作,该网站已经从其 .htaccess 文件中启用了即时 gzip,如下所示:

<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml...
</ifmodule>

修改后的代码

我已经设置了一个构建脚本,它使用 brotli 压缩了许多静态 Assets 。为了服务他们,我将上面的 mod_deflate block 替换为以下内容:

<IfModule mod_headers.c>
    # Serve brotli compressed CSS and JS files if they exist
    # and the client accepts brotli.
    RewriteCond "%{HTTP:Accept-encoding}" "br"
    RewriteCond "%{REQUEST_FILENAME}\.br" "-s"
    RewriteRule "^(.*)\.(js|css)"              "$1\.$2\.br" [QSA]

    # Serve correct content types, and prevent double compression.
    RewriteRule "\.css\.br$" "-" [T=text/css,E=no-brotli:1]
    RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-brotli:1]

    <FilesMatch "(\.js\.br|\.css\.br)$">
        # Serve correct encoding type.
        Header append Content-Encoding br

        # Force proxies to cache brotli &
        # non-brotli css/js files separately.
        Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

问题

当 brotli 编码的文件按预期存在时,它会提供这些文件。然而,我现在面临的问题是,因为剩余的 Assets 在构建时没有进行 brotli 编码,所以它们现在没有压缩。

我一直无法弄清楚如何使用不需要我为 gzip 输出预压缩的 gzip 回退来为 brotli 提供服务。

感谢任何帮助,谢谢!

最佳答案

您的问题是您已将动态 gzip 配置替换为静态配置。

您需要两个配置位,还需要更改您的 Brotli 代码以将环境设置为 no-gzip,这样它就不会回退。以下应该有效;

<ifmodule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml...
</ifmodule>

<IfModule mod_headers.c>
    # Serve brotli compressed CSS and JS files if they exist
    # and the client accepts brotli.
    RewriteCond "%{HTTP:Accept-encoding}" "br"
    RewriteCond "%{REQUEST_FILENAME}\.br" "-s"
    RewriteRule "^(.*)\.(js|css)"              "$1\.$2\.br" [QSA]

    # Serve correct content types, and prevent double compression.
    RewriteRule "\.css\.br$" "-" [T=text/css,E=no-gzip:1]
    RewriteRule "\.js\.br$"  "-" [T=text/javascript,E=no-gzip:1]

    <FilesMatch "(\.js\.br|\.css\.br)$">
        # Serve correct encoding type.
        Header append Content-Encoding br

        # Force proxies to cache brotli &
        # non-brotli css/js files separately.
        Header append Vary Accept-Encoding
    </FilesMatch>
</IfModule>

关于apache - 如何即时更新 .htaccess 以有条件地 gzip,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58171717/

相关文章:

apache - 我如何将经过身份验证的用户从 apache kerberos 传递到 tomcat GET 请求

php - 从具有 .htaccess 限制下载的文件夹中播放 mp3

regex - 如何通过 .htaccess 删除 URL 中任意位置的空参数或参数?

c - 使用 zlib 使用 zpipe.c 示例创建 gzip 文件

linux - Linux 和 Apache 的 ".d"是怎么回事?

php - 替换php文件中的变量

java - < SOAP :header/> block in not coming in soap response

php - 为什么这个 .htaccess 文件不显示存在的文件?

file - gzip Reader.Close 的意义何在?

Java gzip - GZIPOutputStream 不任意关闭/压缩