简体   繁体   English

.htaccess mod_rewrite:通过检查文件是否存在来启用缓存

[英].htaccess mod_rewrite: enabling caching via checking if file exists

I am implementing a caching system for dynamically generated images. 我正在为动态生成的图像实现缓存系统。 The script is called via /img/type/param1/param2.png , where multiple types are possible and the number of parameters are variable. 该脚本通过/img/type/param1/param2.png ,其中可能有多种类型,参数数量是可变的。 The script generates the image and dumps it to disk. 该脚本生成映像并将其转储到磁盘。

I would like .htaccess rules, which when requesting the image generation script: 我想要.htaccess规则,该规则在请求图像生成脚本时:

  1. checks to see if a cached file exists 检查是否存在缓存的文件
  2. if so, mod_rewrite to the cached file 如果是这样,则mod_rewrite到缓存的文件
  3. if not, don't do anything so that the script runs 如果没有,则不执行任何操作以使脚本运行

What I have so far is a slightly modified logic: 到目前为止,我有一个稍微修改的逻辑:

  1. mod_rewrite to where the cached file would be mod_rewrite到缓存文件所在的位置
  2. check to see if the file exists 检查文件是否存在
  3. if not, mod_rewrite the request back again 如果不是,则mod_rewrite请求再次返回

If there's a better way to do this, I'd love to hear it. 如果有更好的方法可以做到这一点,我很想听听。 In any case, the relevant part of my .htaccess file: 无论如何,我的.htaccess文件的相关部分是:

RewriteRule ^img/(type1|type2)/(.*)$ /images/cache/$1/$2

RewriteCond /^(.*)$ !-f
RewriteRule images/cache/(.*) /img/$1

This doesn't seem to work quite right. 这似乎不太正确。 To debug it, I inserted the following after the first RewriteRule (the target page just dumps its input): 为了调试它,我在第一个RewriteRule之后插入了以下内容(目标页面只是转储其输入):

RewriteRule ^(.*)$ /htaccess.php?url=$1 [END]

As a result, I get: 结果,我得到:

Array
(
    [url] => /images/cache/a/l/300.png/a/l/300.png
)

I don't understand why $2 contains l/300.png/a/l/300.png instead of just l/300.png . 我不明白为什么$2包含l/300.png/a/l/300.png而不是l/300.png

If I change first RewriteRule to include an [L] , I get the expected result. 如果我首先更改RewriteRule使其包含[L] ,则将得到预期的结果。 However, I experience the exact same "double-matching" issue on the second RewriteRule which reverts the request back into the non-cache version. 但是,我在第二个RewriteRule上遇到了完全相同的“双重匹配”问题,该问题将请求还原回非缓存版本。 However, adding [L] to this second RewriteRule: 但是,在第二个RewriteRule中添加[L]

RewriteRule ^img/(type1|type2)/(.*)$ /images/cache/$1/$2 [L]

RewriteCond /^(.*)$ !-f
RewriteRule images/cache/(.*) /img/$1 [L]

yields an Internal Server Error. 产生内部服务器错误。

What am I doing wrong, and how do I fix this issue? 我在做什么错,如何解决这个问题?

The first logic that you have is what you want to be doing, that bypasses the possible looping issues. 您拥有的第一个逻辑就是您想做的事情,它绕过了可能的循环问题。 You just need to extract the relevant bits from the %{REQUEST_URI} then backreference them using % references. 您只需要从%{REQUEST_URI}提取相关位,然后使用%引用进行反向引用即可。 So for example: 因此,例如:

RewriteCond %{REQUEST_URI} ^/img/(.*)$
RewriteCond %{DOCUMENT_ROOT}/images/cache/%1 -f
RewriteRule ^img/(.*)$ /images/cache/$1 [L]

or using the regex patterns that you had: 或使用您拥有的正则表达式模式:

RewriteCond %{REQUEST_URI} ^/img/(type1|type2)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/images/cache/%1/%2 -f
RewriteRule ^img/(.*)$ /images/cache/$1 [L]

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

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