Example Kirby setup with kirby/site/content below the web root

If you’re like me, you like putting PHP code outside of the web root root (the one your VirtualHost points to) as much as possible. It’s actually pretty easy to do with kirby, using a folder structure such as:

project-folder/
    content/…
    kirby/…
    site/…
    web/
        assets/…
        .htaccess
        index.php

Note that I’ve done that a few times, but I haven’t tried it yet for the panel.

I was wondering if some people would be interested in coming up with a somewhat “standard” boilerplate, as a github repo similar to https://github.com/getkirby/plainkit but tuned so that the web root is the web (or www or public or something else) root.

1 Like

I prefer this structure as well, but I’ve had problems with the content folder. Since images/files are being stored outside the webroot, they’re showing up as broken links. I tried to do some rerouting using Apache rewrites, but couldn’t come up with a working solution. Have you figured something out?

That’s a good question. I’ve used custom structures like this on two sites. On one I don’t have images or web files. On the other one I actually serve the content folder:

projectroot/
    kirby/
    site/
    web/
        .htaccess
        index.php
        articles/
            cool-article/
                article.txt
                cover.jpg
                other-pic.png

(I fiddled with a .htaccess a little bit.)

So I haven’t really tackled this issue.

One solution is to keep the content folder in the web root:

projectroot/
    kirby/
    site/
    web/
        .htaccess
        index.php
        content/

Another one, with the content folder below the web root, would be to rewrite the URLs pointing to /content/*, but I doubt URL rewriting allows reaching below the web root. So we would have to write a PHP script (or add to index.php) for serving stuff from the content folder, which means some overhead, but is doable. It’s also an opportunity to make relative links work. E.g. if your post content says <img src="cool-pic.svg" alt="Cool stuff">, that’s going to make a request for /some/path/cool-pic.svg, and we can resolve to the actual file.

Other issues with static assets:

  • root/web/assets is only a convention, but maybe an empty assets folder can be useful for people wondering where to put theme assets (not sure what the conventions for themes are, if any).
  • root/web/panel/assets can be a symlink to root/panel/assets.
  • Leave thumbs in root/web/thumbs? Need to change some wiring to make thumb generation work with a different structure?

All of this might be too much trouble, but it’s a fun exercise. Also I think some “advanced” sites will need to do that kind of thing anyway, so it can be useful to have an example around.

Another solution I just thought of - move all your images to a CDN. :smile:
Actually it’s not too bad of an idea, if it could be accomplished seamlessly. If there were some API hooks inside the Kirby panel, it could be done 100% automatically. Otherwise, might have to have some auto-syncronization set up on your web server.

Another idea: this kind of setup could be useful for outputting a Kirby-based website as static files in a folder (e.g. root/static-build). Bastian had the basics of a PHP script doing that.

With symlinks you can reach files/folder below the web root. But perhaps you want to avoid symlinks?

With symlinks you can reach files/folder below the web root. But perhaps you want to avoid symlinks?

Symlinks aren’t out of the question. Would I have to do it for each file, or can the whole folder be linked?

You can link to whole folders, which makes it very handy.
I’m not sure symlinks are as good regarding security though.

For the record, I’ve recently used a setup with symlinks like this:

content/
    articles/
    error/
    home/
    otherstuff/
kirby/
site/
web/
    .htaccess
    index.php
    assets/
    articles -> ../content/articles
    otherstuff -> ../content/otherstuff

That way I’m sure that the kirby and site folders are not accessible (except if there’s a security vulnerability in Kirby or my own code or server, of course). And I create symlinks for the 2-3 main sections of content for which I want to allow direct access to images and other static content.

For instance I can access /articles/my-article/demo-file.svg directly.

Note that with that kind of setup, the .htaccess rule which disallows access to the .txt files in the /content/ folder won’t work anymore. That’s okay for me since I was disabling it on purpose already. If that’s not okay for you, then that RewriteRule will need some modifications.