mod_rewrite RewriteCond based on Last-modified? (.htaccess)

£可爱£侵袭症+ 提交于 2019-12-03 03:35:25

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.

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]

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!