Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Php 使用mod_重写进行缓存_Php_Regex_Apache_.htaccess_Mod Rewrite - Fatal编程技术网

Php 使用mod_重写进行缓存

Php 使用mod_重写进行缓存,php,regex,apache,.htaccess,mod-rewrite,Php,Regex,Apache,.htaccess,Mod Rewrite,我想重新编写一个请求,这样Apache将从文档根目录中的缓存目录中获取它 我需要两个条件来实现这一点: 该文件没有扩展名,因此没有videos.css等 该文件存在于缓存中/ 如果不满足上述条件,我想将请求路由到index.php 我应该在.htaccess中输入什么来实现这一点?尝试以下两条规则: RewriteEngine On #forward to cache/<file> if it exists in %{DOCUMENT_ROOT}/cache RewriteCond

我想重新编写一个请求,这样Apache将从文档根目录中的缓存目录中获取它 我需要两个条件来实现这一点:

  • 该文件没有扩展名,因此没有videos.css等
  • 该文件存在于缓存中/
  • 如果不满足上述条件,我想将请求路由到index.php

    我应该在.htaccess中输入什么来实现这一点?

    尝试以下两条规则:

    RewriteEngine On
    
    #forward to cache/<file> if it exists in %{DOCUMENT_ROOT}/cache
    RewriteCond %{DOCUMENT_ROOT}/cache/$1 -f
    RewriteRule ^([^/.]+)/?$ /cache/$1 [L]
    
    # otherwise forward to /index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{DOCUMENT_ROOT}/cache/$1 !-f
    RewriteRule ^ /index.php [L]
    
    重新编写引擎打开
    #转发到缓存/如果它存在于%{DOCUMENT_ROOT}/cache中
    重写cond%{DOCUMENT_ROOT}/cache/$1-f
    重写规则^([^/]+)/?$/cache/$1[L]
    #否则,请转到/index.php
    重写cond%{REQUEST_FILENAME}-F
    重写cond%{DOCUMENT_ROOT}/cache/$1-F
    重写规则^/index.php[L]
    
    如果您使用
    .html
    扩展名创建缓存文件,那么这种方法可能对您有用

    RewriteCond %{REQUEST_METHOD} GET
    RewriteCond %{QUERY_STRING} ^$
    RewriteCond %{DOCUMENT_ROOT}/cache/%{REQUEST_URI}\.html -f
    RewriteRule .* cache/%{REQUEST_URI}\.html [L]
    

    文件没有扩展名
    在进行全页缓存时,我认为明智的做法是将
    .html
    扩展名附加到缓存的文件中。您正在保存的是html页面,对吗?那么为什么不把它说清楚,让网络服务器识别出来呢。好吧,这是有道理的。+1'因为一个零票的被接受的答案看起来很愚蠢:)这不起作用。当我尝试将其根目录改为index.php时,即使存在缓存/视频。请检查最新编辑的答案。