我有这个文件结构:
/
/index.php
/test.php
/example/foo/bar/test.php
/cache/index.htm
/cache/test.htm
/cache/foo/bar/test.htm
Run Code Online (Sandbox Code Playgroud)
/ cache/*中的所有内容都是生成的php文件的平面文件(.htm).
基本上我想做的就是 -
/index.htm(用户永远不会在他们的网址中看到.php,即使它是动态制作的)/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中这有可能吗?
谢谢
(顺便说一句,我在其他地方制作缓存文件.所以不需要在运行中制作它们)
假设您要做的唯一一种检查是存在检查,应该可以使用以下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
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
379 次 |
| 最近记录: |