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

后端 未结 3 1488
暖寄归人
暖寄归人 2021-02-06 10:38

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 s

3条回答
  •  伪装坚强ぢ
    2021-02-06 11:20

    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]
    

提交回复
热议问题