Image URLs - How to remove string?

Hey everyone,

there are a bunch of reasons, why we went for this solution based on feedback from many users during v2.

In v2, direct URLs to files always included the sorting number of folders. Whenever you changed the sorting of a page the URL to all its files would break. This is less than ideal for SEO and consistent URLs in general. With the new media API you always get the same URL no matter how the pages are sorted. URLs to files only change when the file itself is being modified.

In v2 the content folder had to be within the document root and access had to be public in order to get file URLs working. This made it impossible to move the content folder above the document root to protect the content, which is now possible.

This also means that it’s now possible to write a plugin, which intercepts calls to files from the content folder to avoid accessing files based on permissions for example.

Such a plugin could also intercept calls in general to optimize images or other media files and resize them to a web-compatible size or run cli tools like imageoptim while you would still keep the original version in your content folder.

The hashes in URLs have been a popular request as well because they enable heavy caching configurations on the server with far-future expiry times. This wasn’t possible in v2 as well.

By proxying the calls to files in general it becomes also possible to write plugins that move the files entirely off the server (to S3 for example) and fetch them from there. Or use a service like Imgix or Cloudinary instead of our own media api. This is a huge step forward in our opinion.

If you are still worried about the additional storage space on your server you can quite easily roll back those changes by replacing our file::url component and return a direct link to the file in the content folder again.

A plugin for this would look like this:

<?php

Kirby::plugin('yourname/old-file-urls', [
  'components' => [
    'file::url' => function (Kirby $kirby, $file) {
        return $kirby->url() . '/content/' . $file->parent()->diruri() . '/' . $file->filename();
    },
  ]
]);

(I haven’t tested it but this should be working)

I hope this will help to understand the decision.

9 Likes