[英]Apache: How to enable browser cache for test.json that has rewrite/is generated trough json.php?
How to enable browser cache
for test.json that has rewrite/is generated trough json.php ? 如何为通过json.php生成重写/的test.json启用浏览器
cache
?
Unfortunately test.json responds headers that are set for \\.php$
and not \\.json$
. 不幸的是,test.json响应为
\\.php$
而不是\\.json$
设置的标头。
How to applay .htaccess rules correctly, so that test.json that is generated by json.php will be cached and on browser refresh it'll return 304 Not Modified
? 如何正确地应用.htaccess规则,以便缓存json.php生成的test.json,并在浏览器刷新时返回
304 Not Modified
? And why response header still shows Server: Apache
when I use ServerSignature Off
? 当我使用
ServerSignature Off
时,为什么响应头仍显示Server: Apache
?
test.json stuck on status 200 OK
, response headers: test.json停留在状态
200 OK
,响应头:
Date Thu, 17 Feb 2011 10:24:44 GMT
Server Apache
Content-Encoding gzip
Vary Accept-Encoding
X-Content-Type-Options nosniff
Cache-Control private, max-age=0
Imagetoolbar no
Content-Length 88
Keep-Alive timeout=1, max=100
Connection Keep-Alive
Content-Type application/json; charset=UTF-8
json.php json.php
<?php
header('Content-type: application/json; charset=UTF-8');
echo '{"tets":"json"}';
?>
httpd.conf httpd.conf文件
ServerTokens Prod
KeepAliveTimeout 1
.htaccess 的.htaccess
Options -Indexes +FollowSymLinks -MultiViews
ServerSignature Off
php_value output_handler ob_gzhandler
AddType application/json .json
AddDefaultCharset utf-8
AddCharset utf-8 .json
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE application/x-httpd-php application/json
<IfModule mod_setenvif.c>
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
</IfModule>
</IfModule>
<IfModule mod_gzip.c>
mod_gzip_on Yes
mod_gzip_can_negotiate Yes
mod_gzip_static_suffix .gz
AddEncoding gzip .gz
mod_gzip_update_static No
mod_gzip_keep_workfiles No
mod_gzip_minimum_file_size 500
mod_gzip_maximum_file_size 5000000
mod_gzip_maximum_inmem_size 60000
mod_gzip_min_http 1000
mod_gzip_dechunk Yes
mod_gzip_handle_methods GET POST
mod_gzip_item_include file \.(php|json)$
mod_gzip_item_include mime ^application/x-httpd-php$
mod_gzip_item_include mime ^application/json$
mod_gzip_item_include mime ^httpd/unix-directory$
mod_gzip_item_include handler ^proxy-server$
mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
mod_gzip_send_vary On
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault "access plus 1 month"
ExpiresByType application/x-httpd-php "access plus 0 second"
ExpiresByType application/json "access plus 1 day"
</IfModule>
<IfModule mod_headers.c>
<FilesMatch "\.php$">
Header set Cache-Control "private, max-age=0"
Header set Imagetoolbar no
</FilesMatch>
<FilesMatch "\.json$">
Header set Cache-Control "public, max-age=31536000"
Header append Vary Accept-Encoding
</FilesMatch>
FileETag None
Header unset ETag
Header unset X-Powered-By
Header set X-Content-Type-Options: nosniff
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^test.json json.php [L,QSA]
</IfModule>
To enable chaching add this: 要启用chaching,请添加以下内容:
ExpiresDefault "access plus 1 month"
To prevent caching do this: 要防止缓存,请执行以下操作:
According to http://www.askapache.com/htaccess/using-http-headers-with-htaccess.html#prevent-caching-with-htaccess , add this 根据http://www.askapache.com/htaccess/using-http-headers-with-htaccess.html#prevent-caching-with-htaccess ,添加此项
<FilesMatch "\.json$">
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control "max-age=0, no-cache, no-store, must-revalidate"
Header set Pragma "no-cache"
Header set Expires "Wed, 11 Jan 1984 05:00:00 GMT"
</IfModule>
</FilesMatch>
Thanks @Rich Bradshaw, I found the solution on using PHP header in Answering HTTP_IF_MODIFIED_SINCE and HTTP_IF_NONE_MATCH in PHP 谢谢@Rich Bradshaw,我找到了在PHP中回答HTTP_IF_MODIFIED_SINCE和HTTP_IF_NONE_MATCH时使用PHP头的解决方案
function caching_headers($file,$timestamp){
$gmt_mtime=gmdate('r', $timestamp);
header('ETag: "'.md5($timestamp.$file).'"');
if(isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])||isset($_SERVER['HTTP_IF_NONE_MATCH'])){
if ($_SERVER['HTTP_IF_MODIFIED_SINCE']==$gmt_mtime||str_replace('"','',stripslashes($_SERVER['HTTP_IF_NONE_MATCH']))==md5($timestamp.$file)){
header('HTTP/1.1 304 Not Modified');
exit();
}
}
header('Last-Modified: '.$gmt_mtime);
header('Cache-Control: public');
}
caching_headers($_SERVER['SCRIPT_FILENAME'],filemtime($_SERVER['SCRIPT_FILENAME']));
Probably there are no better/suitable solution. 可能没有更好/合适的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.