Tag Archives: apache

Apache prevent web browsers from caching

In order to get the latest effections, Front-end develop need to refresh the web page frequently.

there some solution for fix page cache problem:

Ensure the request url is unique.

by adding random seed to URL. such as

<script src="http://xx.com/foo/path.js?v=${version}"></script>

But this case need some server programe to generic the html page resouces references dynamic.

The other one and the more simple way is prevent Web browsers by customize HTTP Headers.

Setting a short cache time

By asking the Web browser to only cache the file for a very short length of time, you can usually avoid the problem.

We support the Apache Web server “mod_expires” feature. To use this to set the cache time to just one second, add these lines to a .htaccess file:

ExpiresActive On
ExpiresDefault A1
Header append Cache-Control must-revalidate

That’s all it takes. The next time the visitor views the file, more than a second will have passed, so the browser won’t use the outdated cached copy.

Controlling which files are affected

Disabling browser caching will slow down your site and increase the amount of bandwidth your site uses, because repeat visitors will always connect to your site to re-download files they would otherwise cache.

To minimize that impact, you might want to prevent caching of files that change often (such as HTML files), while allowing normal caching of files that don’t (such as JPEG files).

To do this, include the .htaccess lines in a <FilesMatch> directive. For example, these lines will prevent caching of filenames ending in “.htm” or “.html”, while allowing normal caching of JPEG files:

<FilesMatch "\.(htm|html|js|css)$">
  ExpiresActive On
  ExpiresDefault A1
  Header append Cache-Control must-revalidate
</FilesMatch>

(An alternate way of doing this is to use the Apache “ExpiresByType text/html” command, but that doesn’t allow you to add the “Cache-Control: must-revalidate” header for only those pages.)

For more info about Apache headers see also: Manipulating HTTP Headers with htaccess.

Fast, Effective PHP Compression

Normally, we just need config the apache setting to enable Gzip compression features.

#####
# Enable compression (gzip compression) – Apache Server (httpd)
# (start)
#
<IfModule mod_deflate.c>
    SetOutputFilter DEFLATE
    # file-types indicated will not be compressed
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|rar|zip|pdf)$ no-gzip dont-vary
    <IfModule mod_headers.c>
        Header append Vary User-Agent
    </IfModule>
</IfModule>
<IfModule mod_log_config.c>
    <IfModule mod_deflate.c>
        DeflateFilterNote Input instream
        DeflateFilterNote Output outstream
        DeflateFilterNote Ratio ratio
        SetEnvIf Request_URI \.(?:gif|jpe?g|png|rar|zip|pdf)$ ignore-log
        SetEnvIf Request_URI \.html image-request
        LogFormat '"%r" %{outstream}n/%{instream}n (%{ratio}n%%)' deflate
        CustomLog /var/log/apache2/deflate.log deflate env=!ignore-log
    </IfModule>
</IfModule>

<IfModule mod_expires.c>
    # enable expirations
    ExpiresActive On
    # expire GIF images after a month in the client's cache
    ExpiresByType image/gif "access plus 1 month 15 days 2 hours"
    # test that cached version is not used for the request
    ExpiresByType text/html "access plus 30 seconds"
    # check with cached version
    #ExpiresByType text/html "access plus 30 days"
</IfModule>

##### gzip configuration - (end)

Alternate Method

Place the following code before the (X)HTML content in any PHP script:

<?php if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start(); ?>

In this case, the ob_flush() command is unnecessary as PHP inherently flushes the buffer. The script delivers gzipped content to capable browsers and uncompressed content to incapable browsers.