简体   繁体   English

里面的 Cache-Control 标头<if>和<else> htaccess 文件中的条件被忽略</else></if>

[英]Cache-Control headers inside <If> and <Else> conditionals are being ignored in htaccess file

I've been trying to figure this one out for a few days now and have finally made a simplified enough test environment so that I can post this question with several examples.几天来我一直在尝试解决这个问题,并最终制作了一个足够简化的测试环境,以便我可以通过几个示例发布这个问题。

The problem I'm running into is that my htaccess file is ignoring both the <If> and <Else> conditions.我遇到的问题是我的 htaccess 文件同时忽略了<If><Else>条件。 I am using Apache version 2.4.29 which does support this feature.我正在使用 Apache 版本 2.4.29,它确实支持此功能。 Likewise, when I save the htaccess file, I do not get a 500-level error– the site works just fine (besides these being ignored).同样,当我保存 htaccess 文件时,我没有收到 500 级错误——该站点运行良好(除了这些被忽略)。

My use case is that I'd like to listen for a query parameter and change the Cache-Control header based on whether that query parameter exists.我的用例是我想监听一个查询参数并根据该查询参数是否存在来更改 Cache-Control header。 I'm sure others would disagree with this premise and while I'm curious what a better solution would be, I would like to get to the bottom of this particular issue.我相信其他人会不同意这个前提,虽然我很好奇更好的解决方案是什么,但我想深入了解这个特定问题。

I've tried feeding several conditions to the <If> thinking that maybe it just didn't like the format or that I was doing something wrong there, but I've now simplified it to be simply <If false> to try to force the <Else> side of the conditional.我已经尝试向<If>提供几个条件,认为它可能只是不喜欢这种格式或者我在那里做错了什么,但我现在已经将它简化为简单的<If false>试图强制条件的<Else>侧。 No matter what I do, neither side of the conditional is working– it is simply ignored completely.无论我做什么,条件的任何一方都不起作用——它被完全忽略了。

Am I doing something wrong?难道我做错了什么? I believe my syntax matches the documentation , but maybe I'm missing something... While I do have a lot of experience writing .htaccess files, I will admit I am far from an Apache expert.我相信我的语法与文档相匹配,但也许我遗漏了一些东西......虽然我确实有很多编写 .htaccess 文件的经验,但我承认我与 Apache 专家相去甚远。

Test cases测试用例

I made a webpage that simply loads a single image.png file and I read the Cache-Control header in Chrome DevTools.我制作了一个网页,只加载了一个image.png文件,并在 Chrome DevTools 中读取了 Cache-Control header。


Example A示例 A

.htaccess file contents: .htaccess文件内容:

<FilesMatch "png">
    Header set Cache-Control "max-age=123, public"
</FilesMatch>

<If false>
    <FilesMatch "png">
        Header unset Cache-Control
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    </FilesMatch>
</If>
<Else>
    <FilesMatch "png">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
</Else>

Expected: The Cache-Control max-age should be 31536000预期: Cache-Control max-age应为31536000

Actual: The Cache-Control max-age is 123实际: Cache-Control max-age123

最大年龄为 123


Example B示例 B

.htaccess file contents: .htaccess文件内容:

<If false>
    <FilesMatch "png">
        Header unset Cache-Control
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
    </FilesMatch>
</If>
<Else>
    <FilesMatch "png">
        Header set Cache-Control "max-age=31536000, public"
    </FilesMatch>
</Else>

Expected: The Cache-Control max-age should be 31536000预期: Cache-Control max-age应为31536000

Actual: No Cache-Control header exists at all实际:根本不存在Cache-Control header

根本没有缓存控制标头


Edit: A clue has emerged that I am able to use 301 RedirectMatch inside the If/Else conditions, so it seems like the cache control headers are what is being ignored from within the conditionals... If that is the case, I'm curious what other options I have to make my use case (described above) work.编辑:出现了一条线索,表明我可以在 If/Else 条件中使用 301 RedirectMatch,因此缓存控制标头似乎是在条件中被忽略的内容...如果是这种情况,我好奇我必须使用哪些其他选项才能使我的用例(如上所述)工作。

Edit 2: Another clue is that without the <FilesMatch> line, the Cache-Control max-age header is applied as expected (just not to the right files).编辑 2:另一个线索是,如果没有<FilesMatch>行,Cache-Control max-age header 按预期应用(只是不适用于正确的文件)。

So I found a work-around for my specific use-case which is why I am "answering" my own question, but I am not "accepting" this answer as it does not resolve the core problem.因此,我为我的特定用例找到了一种解决方法,这就是为什么我“回答”我自己的问题,但我不“接受”这个答案,因为它不能解决核心问题。 I wanted to provide this for anyone who stumbles upon this issue in the future.我想为将来偶然发现此问题的任何人提供此功能。

What I found was that <FilesMatch> was what was being ignored inside of <If> and <Else> conditions.我发现<FilesMatch>是在<If><Else>条件中被忽略的内容。 Redirects and headers could be set without a problem within them.可以在其中设置重定向和标头而不会出现问题。

So for my use-case, I flipped the logic around.所以对于我的用例,我翻转了逻辑。 I set the regular cache lengths as I wanted for individual file types using <FilesMatch> (outside of any conditionals).我使用<FilesMatch> (在任何条件之外)为单个文件类型设置了常规缓存长度。 Then, I added an <If> condition to check for the query string, and if true it would unset all Cache-Control headers (so no FilesMatch as it was for all files).然后,我添加了一个<If>条件来检查查询字符串,如果为真,它将取消设置所有 Cache-Control 标头(因此对于所有文件都没有 FilesMatch)。

Here is an example of what I'm now doing for my work-around (in the same format as the above examples):这是我现在为解决方法所做的一个示例(格式与上述示例相同):

# Set the cache headers as desired for individual files
<FilesMatch "png">
    Header set Cache-Control "max-age=123, public"
</FilesMatch>

# When this condition is true unset all of the caches (to be replaced with a query string detection)
<If true>
    # Notice no FilesMatch here– it affects all files
    Header unset Cache-Control
    Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
</If>

Again, this does not actually answer the core question, so I am not going to "accept" my own answer, but I wanted to provide it for anyone else.同样,这实际上并没有回答核心问题,所以我不会“接受”我自己的答案,但我想为其他人提供它。

You may be able to use if/else for .png files like this:您可以将if/else用于.png文件,如下所示:

<FilesMatch "\.png$">
   <If false>
        Header unset Cache-Control
        Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
   </If>
   <Else>
        Header set Cache-Control "max-age=31536000, public"
   </Else>
</FilesMatch>

Expectedly it will set Cache-Control: max-age=31536000, public for all .png files.预计它将为所有.png文件设置Cache-Control: max-age=31536000, public

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM