如果文件存在,如何使用.htaccess从缓存中静默读取?这可能吗?
我有以下文件结构:如果文件存在,如何使用.htaccess从缓存中静默读取?这可能吗?,.htaccess,.htaccess,我有以下文件结构: / /index.php /test.php /example/foo/bar/test.php /cache/index.htm /cache/test.htm /cache/foo/bar/test.htm /cache/*中的所有内容都是生成的php文件的平面文件(.htm) 基本上我想做的就是- 用户请求/index.htm(用户 永远不会在他们的url中看到.php 即使它是在飞行中制造的) .htaccess检查是否/cache/index.htm 存在。
/
/index.php
/test.php
/example/foo/bar/test.php
/cache/index.htm
/cache/test.htm
/cache/foo/bar/test.htm
/cache/*中的所有内容都是生成的php文件的平面文件(.htm)
基本上我想做的就是-
- 用户请求
(用户 永远不会在他们的url中看到.php 即使它是在飞行中制造的)/index.htm
- .htaccess检查是否
存在。如果是这样的话,它是这样写的 文件李>/cache/index.htm
- 如果
没有 存在时,它提供index.php服务/cache/index.htm
- 用户请求
/example/foo/bar/test.htm
- 如果存在
,它将从中读取/cache/example/foo/bar/test.htm
- 如果不存在,则显示用户
(但看不到.php扩展名)/example/foo/bar/test.php
(顺便说一句,我将缓存文件放在其他地方。因此无需动态创建它们)假设您要执行的唯一检查类型是存在性检查,则可以使用以下规则集:
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