Troubleshooting Kirby Project with Public Folder Setup: Server Error and Routing Issues

Hi everyone,

I recently changed my Kirby project to a public folder setup for increased security and to resolve some issues I was having running my project on my server. Unfortunately, it’s still not running on my server, and I keep getting a 500 server error. I’m hosting the website on Netcup and deploying with Git to my master branch. The document root is pointed to the public folder.

Here’s what I did:

  1. Created a “public” folder in my root directory.
  2. Moved index.php, media folder, and robots.txt into the public folder.
  3. Modified index.php like so:
<?php

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

$base = dirname(__DIR__);
$storage = $base . '/storage';

$kirby = new Kirby([
  'roots' => [
    'index' => __DIR__,

    'site' => $base . '/site',
    'content' => $base . '/content',

    'accounts' => $storage . '/accounts',
    'cache' => $storage . '/cache',
    'sessions' => $storage . '/sessions',
    'logs' => $storage . '/logs',
  ]
]);

echo $kirby->render(); 
  1. Modified kirby/router.php like so:
<?php

$uri = urldecode(
	parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// Emulate Apache's `mod_rewrite` functionality
if ($uri !== '/' && file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $uri)) {
	return false;
}

$_SERVER['SCRIPT_NAME'] = '/public/index.php'; // this

require $_SERVER['DOCUMENT_ROOT'] . '/' . $_SERVER['SCRIPT_NAME'];

My questions are:

  1. I use PostCSS, should my node_modules folder be inside the public folder? The package.json is in my project root.
  2. I’m confused about the .htaccess file. Should it be inside the public folder on the server?
  3. Did I catch all necessary changes? I know there is a guideline in the docs but it was not detailed enough for me to understand.

I have two local branches: main and public-folder-setup. The main branch doesn’t render my templates (no HTML, no CSS), and I can only access the panel after switching to my old-setup branch. Otherwise, I just get a white screen when trying to access the panel. Here the .htaccess is inside the public folder.

The public-folder-setup branch has the .htaccess in the root directory and it renders all HTML and CSS.

When running my page locally, the panel login points to http://localhost:8000/public/panel/login, while my start page is simply http://localhost:8000/.

I suspect I’ve messed up some important routing.

My goal for now is to understand these problems and get my project running locally. I want to switch from the internally available Kirby server to MAMP to better emulate the server configurations of the Netcup server. My next step would be to push these changes to Netcup and do further troubleshooting if needed.

Any hints would be greatly appreciated.

  1. You should not touch any of Kirby’s source files, so leave router.php alone
  2. Please post your folder structure

Whoops - ok will do and thanks for pointing that out, here is my folder structure:

Hm, you created a storage folder, but the accounts, cache, sessions folders are still inside site?

Ah yeah true, I overlooked that and deleted them now.

I also adjusted the router.php to how it is in the plainkit:

<?php

$uri = urldecode(
	parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)
);

// Emulate Apache's `mod_rewrite` functionality
if ($uri !== '/' && file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $uri)) {
	return false;
}

$_SERVER['SCRIPT_NAME'] = '/public/index.php';

require $_SERVER['DOCUMENT_ROOT'] . '/' . $_SERVER['SCRIPT_NAME'];

My site runs well on the local server now, but there is still the “public” folder included in the url:
localhost:8000/public/subpage/detail-page
Should I worry about this?

When I run it with MAMP (Apache, PHP 8.2.0) there is no “public” path in the URL but I have issues with the menu:
First level menu item has the correct URL (localhost:8888/subpage)
but the template is missing and the site-title inside the title-Tag does not include the homepage title

When I go to the detail-page (localhost:8888/subpage/detail-page) it works fine

When started how? From the public folder with the built-in server?

Ah yeah sorry, with the built-in server from root:
php -S localhost:8000 kirby/router.php

I just realised the MAMP Server was pointing to a different setup. I will edit that out of my previous post.

MAMP uses the public folder as root, when clicking through the menu all links lead to 404 (Not Found).

With a public folder setup, you need to start the command from the public folder (which is the root folder), the rest is above root

The .htaccess needs to be in the public folder

Super thank you. Now it runs locally on build in server and on mamp, both starting from public folder :slight_smile: