如何使用的.htaccess从缓存中默默念,如果该文件存在?这可能吗? [英] how to use .htaccess to silently read from the cache, if the file exists? is this possible?
本文介绍了如何使用的.htaccess从缓存中默默念,如果该文件存在?这可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有这个文件结构:
/
/index.php
/test.php
/example/foo/bar/test.php
/cache/index.htm
/cache/test.htm
/cache/foo/bar/test.htm
一切都在/缓存/ *是生成的PHP文件的平面文件名(.htm)。
everything in /cache/* is a flat file (.htm) of the generated php files.
基本上我想要做的就是这一点 -
Basically what i want to do is this -
- 在用户请求
将/index.htm
(用户 永远不会看到一个.PHP在他们的网址 即使其在飞行) - 的.htaccess检查是否
/cache/index.htm
存在。如果是这样,从读取 文件。 - 如果
/cache/index.htm
不 存在的,它服务的index.php
- a user requests
/index.htm
(users will never see a .php in their url even if its made on the fly) - .htaccess checks if
/cache/index.htm
exists. if so, it reads from that file. - if
/cache/index.htm
doesn't exist, it serves index.php
另一个例子
- 在用户请求
/example/foo/bar/test.htm
- 如果
/cache/example/foo/bar/test.htm
存在,从中读取 - 如果它不存在,向用户显示
/example/foo/bar/test.php
(但并不见.php扩展)
- a user requests
/example/foo/bar/test.htm
- if
/cache/example/foo/bar/test.htm
exists, it reads from it - if it doesn't exist, the user is shown
/example/foo/bar/test.php
(but doesn't see the .php extension)
这是可能在所有在.htaccess?
is this possible at all in .htaccess?
感谢
(顺便说一句我让其他地方的缓存文件。所以没必要让他们飞)
(btw i make the cache files elsewhere. so no need to making them on the fly)
推荐答案
假设只有同类产品中选中你想要做的是一个存在性检查,应该可以用下面的mod_rewrite~~V 规则集:
Assuming the only kind of check you want to do is an existence check, it should be possible with the following mod_rewrite rule set:
RewriteEngine On
# Check if a PHP page was requested
RewriteCond %{THE_REQUEST} ^[A-Z]+\s/[^\s]+\.php
# If so, redirect to the non-cache .htm version
RewriteRule ^(.*)\.php$ /$1.htm [R=301,L]
# Check if the file exists in the cache
RewriteCond %{DOCUMENT_ROOT}/cache/$0 -f
# If it does, rewrite to the cache file
RewriteRule ^.*$ /cache/$0 [L]
# Check that the file doesn't exist (we didn't go to the cache)
RewriteCond %{REQUEST_FILENAME} !-f
# Check that the request hasn't been rewritten to the PHP file
RewriteCond %{REQUEST_URI} !\.php$
# If both are true, rewrite to the PHP file
RewriteRule ^(.*)\.htm$ /$1.php
这篇关于如何使用的.htaccess从缓存中默默念,如果该文件存在?这可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文