Published on Jun 2, 2026, filed under development. (Share this post, e.g., on Mastodon.)
Have you ever thought about providing a “random article” feature on your website?
And, are you using Eleventy on a PHP-enabled server?
Then this is for you, a quick way of providing a URL to return a random article.
1. Drop the URLs of Your Posts in a PHP File That Randomly Redirects to These URLs
Save the following where you need it (.njk), and adjust where you want to export the output (here: /random/index.php):
---
eleventyExcludeFromCollections: true
permalink: /random/index.php
---
<?php
$posts = [
{%- for post in collections.posts %}
{%- if post.url %}
'{{ post.url | absoluteUrl(metadata.url) }}',
{%- endif %}
{%- endfor %}
];
header('Cache-Control: no-store');
if (!empty($posts)) {
header('Location: ' . $posts[array_rand($posts)], true, 302);
} else {
header('Location: https://meiert.com/', true, 302);
}
exit;In brief, the file:
- makes sure not to be part of other Eleventy collections
- goes through your “posts” collection (change and tailor this where needed)
- (assumes you use Eleventy’s RSS plugin or rolled your own
absoluteUrlfilter) - sets a
Cache-Controlheader (more, as far as I know, shouldn’t be needed anymore) - picks one URL to redirect to on request (or falls back to the homepage, here represented by
metadata.url)
2. Link and Feature the Script
Not much more to see here—link your /random or how you’ve called it, and tell others about the feature!
_ If you want to see this in action, here are three examples: WebGlossary.info uses this solution to provide random terms, meiert.com—this site—gives you random articles of mine, and Haiku Haiku Love, an old heartbreak project where this is done just via PHP, returns random haiku!
About Me
I’m Jens (long: Jens Oliver Meiert), and I’m an engineering lead, guerrilla philosopher, and indie publisher. I’ve worked as a technical lead and engineering manager at various companies (e.g., Google); I’m an active web and tool developer (code optimization, digital defense), a contributor to web standards (like HTML, CSS, WCAG), and a book author (O’Reilly, Frontend Dogma).
I love trying things—in web development and engineering management, but also in politics and philosophy, where I hold to one idea in particular: that we can only be well if we take good care of everyone. Here on meiert.com I talk about some of my perspectives and experiences. (Please share feedback: Interpret charitably, keep it friendly, but do be critical.)

Comments
Nothing yet. Say the first thing.
Sign in to join the conversation.