Static site generator redirect to static files

I try to use Staticache, and the files are generated.

I try to redirect to the static files if they exist. But I don’t get it work for now.

This is my index.php in the root of the installation:

<?php

(function /* staticache */ () {
      $root = __DIR__ . '/site/cache';
    
      // check if a cache for this domain exists
      $root .= '/' . $_SERVER['SERVER_NAME'] . '/pages';
      if (is_dir($root) !== true) {
        return;
      }
    
      // determine the exact file to use
      $path = $root . '/' . ltrim($_SERVER['REQUEST_URI'] ?? '', '/');
      if (is_file($path . '/index.html') === true) {
        // a HTML representation exists in the cache
        $path = $path . '/index.html';
      } elseif (is_file($path) !== true) {
        // neither a HTML representation nor a custom
        // representation exists in the cache
        return;
      }
    
      // try to determine the content type from the static file
      if ($mime = @mime_content_type($path)) {
        header("Content-Type: $mime");
      }
    
      die(file_get_contents($path));
    })();
    

require __DIR__ . '/kirby/bootstrap.php';

echo (new Kirby)->render();

But somehow the site does not redirect to the page, because I don’t see the timestamp on the bottom of the pages after the html tag.

I also changed the htacces file to this:

# Kirby .htaccess
# revision 2023-07-22

# rewrite rules
<IfModule mod_rewrite.c>

# enable awesome urls. i.e.:
# http://yourdomain.com/about-us/team
RewriteEngine on

# make sure to set the RewriteBase correctly
# if you are running the site in a subfolder;
# otherwise links or the entire site will break.
#
# If your homepage is http://yourdomain.com/mysite,
# set the RewriteBase to:
#
# RewriteBase /mysite

# In some environments it's necessary to
# set the RewriteBase to:
#
# RewriteBase /

RewriteCond %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI}/index.html -f [NC]
RewriteRule ^(.*) %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI}/index.html [END]

RewriteCond %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI} -f [NC]
RewriteRule ^(.*) %{DOCUMENT_ROOT}/site/cache/%{SERVER_NAME}/pages/%{REQUEST_URI} [END]

# block files and folders beginning with a dot, such as .git
# except for the .well-known folder, which is used for Let's Encrypt and security.txt
RewriteRule (^|/)\.(?!well-known\/) index.php [L]

# block all files in the content folder from being accessed directly
RewriteRule ^content/(.*) index.php [L]

# block all files in the site folder from being accessed directly
RewriteRule ^site/(.*) index.php [L]

# block direct access to Kirby and the Panel sources
RewriteRule ^kirby/(.*) index.php [L]

# make site links work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [L]

</IfModule>

# pass the Authorization header to PHP
SetEnvIf Authorization "(.+)" HTTP_AUTHORIZATION=$1

# compress text file responses
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE application/json
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript
</IfModule>

# set security headers in all responses
<IfModule mod_headers.c>

# serve files as plain text if the actual content type is not known
# (hardens against attacks from malicious file uploads)
Header set Content-Type "text/plain" "expr=-z %{CONTENT_TYPE}"
Header set X-Content-Type-Options "nosniff"

</IfModule>

But no luck. What am I doing wrong? I use the starterkit now for testing purposes.

Thanks!

I see now that it works on a server root, but when the site is installed in a subfolder it does not work anymore. Any ideas?

  1. The idea is to use either your .htaccess/server config OR the index.php, not both.
  2. In a subfolder, I would expect that you have to adapt the rewrite rules in the htaccess accordingly, but I think it would be better if you put a separate domain/subdomain to this subfolder. Check if the site works in the subfolder as expected without the plugin first.

I copied the site on the root, to a subdomain. Where it worked on the root, it does not work in the subdomain.
I sticked to the index.php version, but the result is the same: it generates the pages, but does not redirect to the static version.

The homepage is in site/cache/static/index.html

index.php

<?php

(function /* staticache */ () {
      $root = __DIR__ . '/site/cache';

      // check if a cache for this domain exists
      $root .= '/static';
      if (is_dir($root) !== true) {
            return;
      }

      // determine the exact file to use
      $path = $root . '/' . ltrim($_SERVER['REQUEST_URI'] ?? '', '/');
      if (is_file($path . '/index.html') === true) {
            // a HTML representation exists in the cache
            $path = $path . '/index.html';
      } elseif (is_file($path) !== true) {
            // neither a HTML representation nor a custom
            // representation exists in the cache
            return;
      }

      // try to determine the content type from the static file
      if ($mime = @mime_content_type($path)) {
            header("Content-Type: $mime");
      }

      die(file_get_contents($path));
})();

require 'kirby/bootstrap.php';

echo (new Kirby)->render();

config.php:


	'cache' => [
		'pages' => [
			'active' => true,
			'type'   => 'static',
			'prefix' => 'static'
		]
	],

Any ideas? Thanks again!!