PHP: 缓存系统创建一个额外的 404 错误文件
PHP: Caching system creates an extra 404 error file
我的 php 网站上有一个缓存系统。
它就像一个魅力,但每当创建缓存文件时,也会从 404 页面生成一个额外的缓存文件。
示例:我访问第 2 页,缓存系统检测到需要创建一个新的 cache-file。它为第 2 页创建文件(并将在稍后将其提供给其他人),但也为 404 页面创建文件。
问题:404cache-page为什么会产生?
我试图找出原因,但我现在卡住了...
这是我的代码 header 包括:
<?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}/ [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= [L]
RewriteRule ^bam/([^/\.]+)/([^/\.]+)/?$ /artists.php?a=&s= [L]
RewriteRule ^browse/([^/\.]+)/?$ /browse.php?l= [L]
查询字符串也可能会传递给 404 处理程序脚本,这会导致生成新的缓存文件。测试是否确实如此:
//$cachefile = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$cachefile = $_SERVER['HTTP_HOST'] . strtok($_SERVER["REQUEST_URI"],'?');
$urlpage = $cachefile;
$cachefile = md5($cachefile).".html";
我认为您的问题是浏览器正在从您的服务器加载资源。例如,检查 404 页面是否来自 favicon.ico 资源。否则尝试将其放入您的“404.php”文件并检查您的缓存中是否有原始 url.
var_dump($_SERVER);
最后我自己找到了问题!
这是将 css 文件导入 css 文件。导入的文件不存在。 Apache 服务器日志没有记录任何错误,因为它记录了到 404 文件的 302 重定向 ...
因此,当我在记录 404 文件之前看到 302 重定向的模式时,我发现导入是错误的。
故事结束:不存在的可疑文件的 302 重定向导致奇怪的 404 错误 :)
我的 php 网站上有一个缓存系统。 它就像一个魅力,但每当创建缓存文件时,也会从 404 页面生成一个额外的缓存文件。
示例:我访问第 2 页,缓存系统检测到需要创建一个新的 cache-file。它为第 2 页创建文件(并将在稍后将其提供给其他人),但也为 404 页面创建文件。
问题:404cache-page为什么会产生?
我试图找出原因,但我现在卡住了...
这是我的代码 header 包括:
<?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}/ [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= [L]
RewriteRule ^bam/([^/\.]+)/([^/\.]+)/?$ /artists.php?a=&s= [L]
RewriteRule ^browse/([^/\.]+)/?$ /browse.php?l= [L]
查询字符串也可能会传递给 404 处理程序脚本,这会导致生成新的缓存文件。测试是否确实如此:
//$cachefile = $_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$cachefile = $_SERVER['HTTP_HOST'] . strtok($_SERVER["REQUEST_URI"],'?');
$urlpage = $cachefile;
$cachefile = md5($cachefile).".html";
我认为您的问题是浏览器正在从您的服务器加载资源。例如,检查 404 页面是否来自 favicon.ico 资源。否则尝试将其放入您的“404.php”文件并检查您的缓存中是否有原始 url.
var_dump($_SERVER);
最后我自己找到了问题!
这是将 css 文件导入 css 文件。导入的文件不存在。 Apache 服务器日志没有记录任何错误,因为它记录了到 404 文件的 302 重定向 ...
因此,当我在记录 404 文件之前看到 302 重定向的模式时,我发现导入是错误的。
故事结束:不存在的可疑文件的 302 重定向导致奇怪的 404 错误 :)