我在我的php网站上有一个缓存系统。它的工作原理就像一个魅力,但是每当创建一个缓存文件时,也会从404页面生成一个额外的缓存文件。
我访问页面2,缓存系统检测到需要创建一个新的缓存文件.它为第2页创建文件(并将为其他人提供迟到),但也为404页创建一个文件。
问题:为什么要创建404缓存页?
我试着找出原因,但我现在被困住了.
这是来自我的标题的代码,包括:
<?php
$url = $_SERVER["SCRIPT_NAME"];
$break = Explode('/', $url);
$file = $break[count($break) - 1];
$cachefile = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$urlpage = $cachefile;
$cachefile = md5($cachefile).".html";
if (!empty($param1) ){$cachetime = 86400;}
elseif (!empty($param2) ){$cachetime = 86400;}
elseif (!empty($param3) ){$cachetime = 86400;}
elseif ($page == 'random' ){$cachetime = 0;}
else {$cachetime = 60;}
$cachefileloc = __DIR__ . '/../_cache/' .$cachefile;
// Serve from the cache if it is younger than $cachetime
if (file_exists($cachefileloc) && time() - $cachetime < filemtime($cachefileloc)) {
include($cachefileloc);
exit;
}
ob_start(); // Start the output buffer
<my html-output starts here>这是我页脚的代码,包括:
<?php
// Cache the contents to a file
$cached = fopen(__DIR__ . '/../_cache/' .$cachefile, "w");
fwrite($cached, ob_get_contents());
fclose($cached);
ob_end_flush(); // Send the output to the browser
?>对这个问题有什么想法吗?
额外信息:- 404页总是在创建缓存文件之后创建的。- 404页并不总是创建的,看起来googlebot访问并不是在创建404。
我的.htaccess:
ErrorDocument 404 http://www.EXAMPLE.com/404.php
###################################################
# Turn the RewriteEngine on. #
###################################################
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^(/)$ /index.php?p= [L]
RewriteRule ^(/)?contact/?$ /index.php?p=contact [L]
RewriteRule ^(/)?aboutus/?$ /index.php?p=aboutus [L]
RewriteRule ^(/)?links/?$ /index.php?p=links [L]
RewriteRule ^(/)?searchresults/?$ /index.php?p=searchresults [L]
RewriteRule ^(/)?contactmail/?$ /index.php?p=mailing [L]
RewriteRule ^(/)?random/?$ /index.php?p=random [L]
RewriteRule ^(/)?recent/?$ /index.php?p=recent [L]
RewriteRule ^(/)?trends/?$ /index.php?p=trends [L]
RewriteRule ^(/)?cookies/?$ /index.php?p=cookies [L]
RewriteRule ^bam/([^/\.]+)/?$ /artists.php?a=$1 [L]
RewriteRule ^bam/([^/\.]+)/([^/\.]+)/?$ /artists.php?a=$1&s=$2 [L]
RewriteRule ^browse/([^/\.]+)/?$ /browse.php?l=$1 [L]发布于 2016-06-07 06:45:25
最后我自己发现了这个问题!
它是将css文件导入到css文件中。导入的文件不存在。apache服务器日志没有记录错误,因为它记录了302重定向到404文件.
所以,当我在404文件被记录之前看到302重定向的模式时,我发现了错误的导入。
故事的结尾: 302个不存在的鱼文件重定向导致了奇怪的404错误:)
发布于 2016-05-29 08:41:45
查询字符串也可能传递给404处理程序脚本,从而生成一个新的缓存文件。为了检验是否真的是这样:
//$cachefile = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$cachefile = $_SERVER['HTTP_HOST'] . strtok($_SERVER["REQUEST_URI"],'?');
$urlpage = $cachefile;
$cachefile = md5($cachefile).".html";发布于 2016-06-04 13:42:56
我认为您的问题是浏览器正在从您的服务器加载资源。例如,检查404页是否来自favicon.ico资源。否则,尝试将其放入您的“404.php”文件中,并检查缓存中的原始url。
var_dump($_SERVER);https://stackoverflow.com/questions/37430284
复制相似问题