Caching css, js, txt files processed by php

Question

I am experimenting a bit and can use some help.
I created 2 files. main-real.css , which is a standard simple css css file and main.css , which is parsed by PHP and has include()that captures the old real css file.
Here is the code for main.css :

<?php 
include("main-real.css");
?>

Then I add the instruction to my .htaccess file to parse this css file using PHP:

<FilesMatch "main.css">
AddHandler application/x-httpd-php5 .css
Header Set Content-Type "text/css"
</FilesMatch> 

This works fine on my PHP 5.2 server with Apache.
The problem is that this file is not cached by the browser or at least it does not return a 304 Not Modifiedstatus code, such as a regular CSS parsing file that does not contain PHP.

Below are the headers for main-real.css , if available directly:

RESPONSE HEADERS
    Date .............. Thu, 18 Nov 2010 22:10:57 GMT
    Server ............ Apache / 2.2.14 (Unix) mod_ssl / 2.2.14 OpenSSL / 0.9.8i DAV / 2 mod_auth_passthrough / 2.1 mod_bwlimited / 1.4 FrontPage / 5.0.2.2635
    Last-Modified ..... Thu, 18 Nov 2010 22:10:23 GMT
    Etag .............. "11b010a-26-4955b0e6671c0"
    Accept-Ranges ..... bytes
    Content-Length .... 38
    Content-Type ...... text / css

REQUEST HEADERS
    Accept.............text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language....en-us,en;q=0.5
    Accept-Encoding....gzip,deflate
    Accept-Charset.....ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive.........115
    Connection.........keep-alive
    Cookie.............fc=fcVal=7625790752294348480
    If-Modified-Since..Thu, 18 Nov 2010 22:10:23 GMT
    If-None-Match......"11b010a-26-4955b0e6671c0"
    Cache-Control......max-age=0

PHP main.css:

RESPONSE HEADERS
    Date...............Thu, 18 Nov 2010 22:11:11 GMT
    Server.............Apache/2.2.14 (Unix) mod_ssl/2.2.14 OpenSSL/0.9.8i DAV/2 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
    X-Powered-By.......PHP/5.2.11
    Content-Type.......text/css
    Keep-Alive.........timeout=5, max=97
    Connection.........Keep-Alive
    Transfer-Encoding..chunked

REQUEST HEADERS
    Accept.............text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
    Accept-Language....en-us,en;q=0.5
    Accept-Encoding....gzip,deflate
    Accept-Charset.....ISO-8859-1,utf-8;q=0.7,*;q=0.7
    Keep-Alive.........115
    Connection.........keep-alive
    Cookie.............fc=fcVal=7625790752294348480
    Cache-Control......max-age=0

http- , max-age, last-modified . -, ?


, Last-Modified include(). PHP ! Last-Modified .htaccess Header set, , .
main.css Expires Cache-Control .

<?php
$last_modified = date("D, d M Y H:i:s \G\M\T", filemtime("main-shared.css"));
$expiration = date("D, d M Y H:i:s \G\M\T", strtotime('+1 year'));

header("Cache-Control: public, no-transform");
header("Expires: $expiration");
header("Last-Modified: $last_modified");

include("main-shared.css");
?>
+3
2

, Apache main-real.css, , include() .

PHP script, .

header("Cache-Control: ........ ");
header("Expires: ....... ");
....
include("main-real.css"); 
+1

HTTP- , CSS . , If-Modified-Since. , :

$last_modified = filemtime("main-real.css");
if(isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])) {
    $expected_modified = strtotime(preg_replace('/;.*$/','',$_SERVER["HTTP_IF_MODIFIED_SINCE"]));
    if($last_modified <= $expected_modified) {
        header("HTTP/1.0 304 Not Modified");
        return;
    }
}
+1

Source: https://habr.com/ru/post/1775750/


All Articles