首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PHP:缓存系统创建一个额外的404错误文件

PHP:缓存系统创建一个额外的404错误文件
EN

Stack Overflow用户
提问于 2016-05-25 07:18:08
回答 3查看 215关注 0票数 1

我在我的php网站上有一个缓存系统。它的工作原理就像一个魅力,但是每当创建一个缓存文件时,也会从404页面生成一个额外的缓存文件。

我访问页面2,缓存系统检测到需要创建一个新的缓存文件.它为第2页创建文件(并将为其他人提供迟到),但也为404页创建一个文件。

问题:为什么要创建404缓存页?

我试着找出原因,但我现在被困住了.

这是来自我的标题的代码,包括:

代码语言:javascript
复制
<?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>

这是我页脚的代码,包括:

代码语言:javascript
复制
<?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:

代码语言:javascript
复制
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]
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-06-07 06:45:25

最后我自己发现了这个问题!

它是将css文件导入到css文件中。导入的文件不存在。apache服务器日志没有记录错误,因为它记录了302重定向到404文件.

所以,当我在404文件被记录之前看到302重定向的模式时,我发现了错误的导入。

故事的结尾: 302个不存在的鱼文件重定向导致了奇怪的404错误:)

票数 0
EN

Stack Overflow用户

发布于 2016-05-29 08:41:45

查询字符串也可能传递给404处理程序脚本,从而生成一个新的缓存文件。为了检验是否真的是这样:

代码语言:javascript
复制
//$cachefile = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$cachefile = $_SERVER['HTTP_HOST'] . strtok($_SERVER["REQUEST_URI"],'?');
$urlpage = $cachefile;
$cachefile = md5($cachefile).".html";
票数 0
EN

Stack Overflow用户

发布于 2016-06-04 13:42:56

我认为您的问题是浏览器正在从您的服务器加载资源。例如,检查404页是否来自favicon.ico资源。否则,尝试将其放入您的“404.php”文件中,并检查缓存中的原始url。

代码语言:javascript
复制
var_dump($_SERVER);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37430284

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档