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. (Interpret charitably but be critical—and share feedback and advice that makes my work better.)
Learn More
- Next: Releasing Middleman Alerter, a Chromium Extension to Warn About Third-Party Reverse Proxies
- Previous: On the Twisted Logic of Labeling Criticism of Israel as “Anti-Semitism”
- Related topics: 2026, development
- Related posts:
- Most popular posts
- Random post
You can support my work for the community by sharing it, by reading or gifting my books, or by becoming a backer on GitHub or Steady. (Let’s talk if you need a competent hand for technical optimization and activism projects.)
The people under attack in Palestine 🇵🇸, Iran 🇮🇷, and Lebanon 🇱🇧—with tens of millions sanctioned, millions displaced, hundreds of thousands injured, and tens of thousands of innocents killed by Israel and the United States—are people like us. They’re our brothers and sisters, too. Read up on their history, scrutinize what you’re told, and demand respect and accountability.
