如何仅为 Apache conf 中使用的现有文件设置 HTTP 标头

发布于 2024-12-10 20:05:13 字数 293 浏览 0 评论 0原文

我使用 Apache 提供文件和应用程序请求,应用程序请求使用 RewriteRule 将与模式匹配的 URL 发送到我的应用程序服务器。

我想仅为与上面的 RewriteConds 不匹配的请求设置特定的 HTTP 标头(缓存控制)。有没有办法在不使用 .htaccess 文件的情况下做到这一点?

我尝试将 Header set 指令放在 RewriteRule (使用 L 标志)之后,但标头仍然被应用。

I'm serving files and app requests using Apache, App requests use RewriteRule to send urls that match a pattern to my app servers.

I'd like to set a particular HTTP header (Cache-control) only for requests that don't match the RewriteConds from above. Is there any way of doing that without using an .htaccess file?

I've tried placing the Header set directive AFTER the RewriteRule (which uses the L flag) but the header still gets applied.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寄人书 2024-12-17 20:05:13

你真的应该发布你的重写规则。

我们当然可以为此做一些事情。我首先想到的是在匹配 rewriteRules 中设置一个环境变量(因此一组 rewriteCond,反转之前的),使用 [E=myvar:myvalue] 标签;并使用 Header 指令 [env=myvar] 的最后一个可用参数来有条件地设置此 Header。

然后你说:

有没有办法在不使用 .htaccess 文件的情况下做到这一点?

好吧,RewriteRules 和 .htaccess 文件是松散相关的。你在 .htaccess 中设置的任何内容都更适合 指令,并在 VirtualHost 上使用一个很好的 AllowOverride None 来防止来自 apache 的大量 IO 操作,尝试在每次请求时读取一堆 .htaccess 文件。

第二件事,你说:

我尝试将 Header set 指令放在 RewriteRule 之后
(使用 L 标志)

好吧,[L] 标志可防止同一目录上的多个 RewriteRules 相继应用,因此它们现在不会直接链接。但事实上,在 rewriteRule 结束 ([L]) 后,rewriteRule 的输出将重新应用于目录,以查看是否仍应应用任何 RewriteRule(可能是同一个)。

这就是为什么我们有时会看到太多内部重定向消息。

看到这种情况发生的唯一方法是在 VirtualHost 上使用 RewriteLog /a/file.logRewriteLogLevel 9 (有时会带来很多惊喜,某些规则有时会起作用)有太多太多的递归)。

一个好的 RewriteCond 众所周知:

RewriteCond %{ENV:REDIRECT_STATUS} ^$

任何以此 rewriteCond 为前缀的规则只会在您第一次进入目录时应用,在规则结束后(可能带有 [L] 或因为没有更多规则),结果将重新测试目录规则,但环境变量 REDIRECT_STATUS 不为空。如果设置了此环境变量,则此条件告诉 mod-rewrite 避免该规则。

You should really post your rewriteRules.

There is certainly something we can do for that. First thing I'm thinking is setting an environment variable in matching rewriteRules (so a set of rewriteCond, inverting the previous ones), use the [E=myvar:myvalue] tag; and use the last available argument of the Header instruction [env=myvar] to conditionnaly set this Header.

Then you said:

Is there any way of doing that without using an .htaccess file?

Well, RewriteRules and .htaccess files are loosely related. Anything you set in a .htaccess would really fit better in a <Directory> instruction with a nice AllowOverride None on your VirtualHost to prevent a looooot of IO operations from apache, trying to read a bunch of .htaccess files at each requests.

Second thing, you said:

I've tried placing the Header set directive AFTER the RewriteRule
(which uses the L flag)

Well the [L] flag prevent several RewriteRules on the same directory to be applied one after the other, so they won't be chained right now direclty. But in fact after your rewriteRule ends ([L]) the output of the rewriteRule is then reapplied on the directory, to see if any RewriteRule (maybe the same one) should still be applied.

This is why we sometimes see too much Internal redirections messages.

The only way to see it happen is to use the RewriteLog /a/file.log and RewriteLogLevel 9 on the VirtualHost (a lot of surprises sometimes, some rules are sometimes working with a lot too much recursions).

A good RewriteCond to known for that is :

RewriteCond %{ENV:REDIRECT_STATUS} ^$

Any rule prefixed with this rewriteCond will only be applied thefirst time you enter the directory, after the end of your rules (maybe with an [L] or because there is not more rule) the result will be re-tested on the direcory rules but with an env variable REDIRECT_STATUS not empty. This Condition tell mod-rewrite to avoid the rule if this env variable is set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文