mod_rewrite RewriteCond基于Last-modified? (.htaccess)

发布于 2024-08-03 05:53:05 字数 224 浏览 5 评论 0原文

我知道我们可以轻松地将 RewriteCond 建立在任何 http request 标头上。但是我们可以检查(一些)将要发送的响应标头吗?特别是最后修改那个?

我只想在上次修改日期早于 30 分钟时重写 url,并且我试图避免每次请求该目录中的文件时将该检查委托给 php 文件的开销。

提前致谢!

I know that we can easily base a RewriteCond on any http request header. But can we check (some of) the response headers that are going to be sent? In particular, the Last-modified one?

I want to rewrite a url only when the Last-modified date is older than 30 minutes and I'm trying to avoid the overhead of delegating that check to a php file every single time a file from that directory is requested.

Thanks in advance!

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

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

发布评论

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

评论(3

若水微香 2024-08-10 05:53:05

不,那不可能。但是您可以使用 重写映射 从比 PHP 开销更少的程序,可能是 shell 脚本。

这是一个示例 bash 脚本:

#!/usr/bin/env bash
while read line; do
    max_age=${line%%:*}
    filename=${line#*:}
    if [[ -f $filename ]]; then
        lm=$(stat -f %m "$filename")
        if [[ $(date +%s)-$lm -le $max_age ]]; then
            echo yes
        else
            echo no
        fi
    else
        echo no
    fi
done

重写映射的声明需要放置在您的服务器或虚拟主机配置文件中,因为程序只启动一次,然后等待输入:

RewriteMap last-modified-within prg:/absolute/file/system/path/to/last-modified-within.sh

然后您可以像这样使用重写映射(.htaccess例子):

RewriteCond %{last-modified-within:30:%{REQUEST_FILENAME}} =yes
RewriteRule ^foo/bar$ - [L]
RewriteRule ^foo/bar$ script.php [L]

No, that’s not possible. But you could use a rewrite map to get that information from a program with less overhead than PHP, maybe a shell script.

Here’s an example bash script:

#!/usr/bin/env bash
while read line; do
    max_age=${line%%:*}
    filename=${line#*:}
    if [[ -f $filename ]]; then
        lm=$(stat -f %m "$filename")
        if [[ $(date +%s)-$lm -le $max_age ]]; then
            echo yes
        else
            echo no
        fi
    else
        echo no
    fi
done

The declaration of the rewrite map needs to be placed in your server or virtual host configuraion file as the program is just started once and then waits for input:

RewriteMap last-modified-within prg:/absolute/file/system/path/to/last-modified-within.sh

And then you can use that rewrite map like this (.htaccess example):

RewriteCond %{last-modified-within:30:%{REQUEST_FILENAME}} =yes
RewriteRule ^foo/bar$ - [L]
RewriteRule ^foo/bar$ script.php [L]
残疾 2024-08-10 05:53:05

出站标头直到 mod_rewrite 生效很久后才存在。 mod_rewrite 中也没有内置任何文件修改时间检查功能,因此您最接近的使用方式就是创建一个 RewriteMap 外部重写程序类型,用于查明相关文件是否已被修改。

如果我正确理解您的应用程序,您还可以考虑让 cron 作业删除该目录中超过 30 分钟的文件,然后在文件不存在的情况下重写。

The outbound headers do not exist until much later than mod_rewrite is acting. There also isn't any file-modification-time checking functionality built into mod_rewrite, so the closest you'd get using it is making a RewriteMap of the External Rewriting Program variety to find out whether the file in question has been modified.

If I understand your application correctly, you could also look into having a cron job delete files in that directory that are older than 30 minutes, and then rewriting on a file-nonexistence condition.

落花浅忆 2024-08-10 05:53:05

您是否考虑过使用 mod_proxy、mod_cache 和/或鱿鱼?听起来您正在尝试滚动自己的缓存......

Have you considered using mod_proxy, mod_cache, and/or squid? It sounds like you're trying to roll your own caching...

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