Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如果文件存在,如何使用.htaccess从缓存中静默读取?这可能吗?_.htaccess - Fatal编程技术网

如果文件存在,如何使用.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)

基本上我想做的就是-

  • 用户请求
    /index.htm
    (用户 永远不会在他们的url中看到.php 即使它是在飞行中制造的)
  • .htaccess检查是否
    /cache/index.htm
    存在。如果是这样的话,它是这样写的 文件
  • 如果
    /cache/index.htm
    没有 存在时,它提供index.php服务
另一个例子

  • 用户请求
    /example/foo/bar/test.htm
  • 如果存在
    /cache/example/foo/bar/test.htm
    ,它将从中读取
  • 如果不存在,则显示用户
    /example/foo/bar/test.php
    (但看不到.php扩展名)
这在.htaccess中完全可能吗

谢谢


(顺便说一句,我将缓存文件放在其他地方。因此无需动态创建它们)

假设您要执行的唯一检查类型是存在性检查,则可以使用以下规则集:

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