如果文件存在,如何使用 .htaccess 从缓存中静默读取?这可能吗?

发布于 2024-09-24 17:51:56 字数 744 浏览 0 评论 0原文

我有这样的文件结构:

/
/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 (用户 永远不会在他们的网址中看到 .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?

谢谢

(顺便说一句,我在其他地方制作了缓存文件。所以不需要即时制作它们)

i have this file structure:

/
/index.php
/test.php

/example/foo/bar/test.php

/cache/index.htm
/cache/test.htm
/cache/foo/bar/test.htm

everything in /cache/* is a flat file (.htm) of the generated php files.

Basically what i want to do is this -

  • 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

another example

  • 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)

is this possible at all in .htaccess?

thanks

(btw i make the cache files elsewhere. so no need to making them on the fly)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云淡月浅 2024-10-01 17:51:56

假设您想要执行的唯一检查是存在检查,则应该可以使用以下 mod_rewrite 规则集:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文