I have written a simple StaticCache Plugin, which saves $this->app->getBody() as a HTML file and redirects to this file via .htaccess.

My Problem now is that it caches the 404 site as index.html.

Where can I get the status-code for the currently displayed page?

$this->app->getHeaders() didn't returned the expected headers...


public function onAfterRender()
    $url = (isset($_SERVER['HTTPS']) ? "https" : "http").'://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

    // Don't cache the admin area, maybe this can be improved also.
    if (strpos($url, 'administrator') !== false) {

    $urlParts = parse_url($url);
    $uri = $urlParts['path'] !== '/' ? $urlParts['path'] : '/index';
    $uri = str_replace(['.html', '.php'], '', $uri).'.html';
    $file = JPATH_BASE . "/cache/static$uri";

    if (!is_dir(dirname($file))) {
        mkdir(dirname($file), 0777, true);
    file_put_contents($file, $this->app->getBody());


RewriteEngine On

# Rewrite to html cache if it exists and the request is off a static page (no url query params and only get requests)
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{DOCUMENT_ROOT}/cache/static%{REQUEST_URI}.html -f
RewriteRule ^(.*)$  /cache/static%{REQUEST_URI}.html [L]

# This is specific to cache the index when it' accesed vie /
RewriteCond %{QUERY_STRING} !.*=.*
RewriteCond %{DOCUMENT_ROOT}/cache/static/index.html -f
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /cache/static/index.html [L]

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.