阿帕奇:如何启用test.json有重写/生成槽json.php浏览器缓存?重写、缓存、浏览器、阿帕奇

由网友(不美不萌照样拽)分享简介:如何启用浏览器缓存的test.json已重写/生成槽json.php?不幸的是test.json回应说是为设置\ PHP $ ,而不是 \。JSON $ 。如何正确applay的.htaccess规则,因此由json.php生成test.json将被缓存并刷新浏览器,它会返回 304未修改 ?为什么响应头仍显示服务器...

如何启用浏览器缓存的test.json已重写/生成槽json.php? 不幸的是test.json回应说是为设置 PHP $ ,而不是 。JSON $

如何正确applay的.htaccess规则,因此由json.php生成test.json将被缓存并刷新浏览器,它会返回 304未修改 ?为什么响应头仍显示服务器:Apache的当我使用 ServerSignature关闭

test.json停留在状态 200 OK ,响应头:

 日期星期四,2011年2月17日10点24分44秒格林尼治标准​​时间
服务器阿帕奇
内容编码的gzip
具体费用接受编码
的X内容类型,选项nosniff
缓存控制私人,最大年龄= 0
Imagetoolbar无
内容长度88
保持活动超时= 1,最大值= 100
连接保持
内容类型的应用程序/ JSON;字符集= UTF-8
 

json.php

 < PHP
    标题(内容类型:应用程序/ JSON;字符集= UTF-8);
    回声{毒鼠强:JSON};
?>
 

的httpd.conf

  ServerTokens PROD
的KeepAliveTimeout 1
 
cocos creater 2.4.11读取json文件

的.htaccess

 选项-Indexes +的FollowSymLinks -MultiViews
ServerSignature关闭
php_value output_handler ob_gzhandler
将AddType应用/ JSON .json
AddDefaultCharset UTF-8
AddCharset UTF-8 .json
< IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE应用/ X的httpd  -  PHP应用程序/ JSON
    < IfModule mod_setenvif.c>
        BrowserMatch ^的Mozilla / 4 GZIP只-text / html的
        BrowserMatch ^的Mozilla / 4  .0 [678]无GZIP
        BrowserMatch  bMSIE!没有GZIP!GZIP只-text / html的
    < / IfModule>
< / IfModule>
< IfModule mod_gzip.c>
    mod_gzip_on是
    mod_gzip_can_negotiate是
    mod_gzip_static_suffix。广州
    AddEncoding GZIP。广州
    mod_gzip_update_static否
    mod_gzip_keep_workfiles否
    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是
    mod_gzip_handle_methods GET POST
    mod_gzip_item_include文件(PHP | JSON)。$
    mod_gzip_item_include哑剧^应用/ X的httpd  -  PHP的$
    mod_gzip_item_include哑剧^应用/ JSON $
    mod_gzip_item_include哑剧^的httpd / UNIX目录$
    mod_gzip_item_include处理^代理服务器$
    mod_gzip_item_exclude rspheader ^内容编码:*。* GZIP
    mod_gzip_send_vary在
< / IfModule>
< IfModule mod_expires.c>
    ExpiresActive在
    ExpiresDefault进入加1月
    ExpiresByType应用/ X的httpd  -  PHP的连接以及0秒
    ExpiresByType应用/ JSON的访问加1天
< / IfModule>
< IfModule mod_headers.c>
    < FilesMatch .PHP $>
        头集缓存控制的私人,最大年龄= 0
        头设置Imagetoolbar没有
    < / FilesMatch>
    < FilesMatch .json $>
        头集缓存控制公,最大年龄= 31536000
        头追加因人而异接受编码
    < / FilesMatch>
    FileETag无
    头取消设置的ETag
    头取消设置的X供电,通过
    头集X-Content-Type的选项:nosniff
< / IfModule>
< IfModule mod_rewrite.c>
    RewriteEngine叙述上
    的RewriteCond%{} REQUEST_FILENAME!-f
    的RewriteCond%{} REQUEST_FILENAME!-d
    重写规则^ test.json json.php [L,QSA]
< / IfModule>
 

解决方案

感谢@Rich布拉德肖,我找到了解决方案,使用PHP头中Answering HTTP_IF_MODIFIED_SINCE和HTTP_IF_NONE_MATCH在PHP

 函数caching_headers($文件,$时间戳){
    $ gmt_mtime = gmdate('R',$时间戳);
    头('ETag的:'。.md5($时间戳$文件)。');
    如果(使用isset($ _ SERVER ['HTTP_IF_MODIFIED_SINCE'])||使用isset($ _ SERVER ['HTTP_IF_NONE_MATCH'])){
        如果($_SERVER['HTTP_IF_MODIFIED_SINCE']==$gmt_mtime||str_replace('"','',stripslashes($_SERVER['HTTP_IF_NONE_MATCH']))==md5($timestamp.$file)){
            头(HTTP / 1.1 304未修改);
            出口();
        }
    }
    头('的Last-Modified:$ gmt_mtime);
    标题(缓存控制:公共');
}
caching_headers($ _ SERVER ['SCRIPT_FILENAME'],filemtime($ _ SERVER ['SCRIPT_FILENAME']));
 

也许没有更好的/合适的解决方案。

How to enable browser cache for test.json that has rewrite/is generated trough json.php ? Unfortunately test.json responds headers that are set for .php$ and not .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? And why response header still shows Server: Apache when I use ServerSignature Off?

test.json stuck on status 200 OK, response headers:

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

<?php
    header('Content-type: application/json; charset=UTF-8');
    echo '{"tets":"json"}';
?>

httpd.conf

ServerTokens Prod
KeepAliveTimeout 1

.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>

解决方案

Thanks @Rich Bradshaw, I found the solution on using PHP header in Answering HTTP_IF_MODIFIED_SINCE and HTTP_IF_NONE_MATCH in 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.

阅读全文

相关推荐

最新文章