Serve Files from Domain Root in Wordpress

Sometimes you want to serve files directly from the root of your domain name, for instance www.domain.com/ads.txt. I also often use my sites as a filedrop, to share working copies of songs etc.

If your site is built in Wordpress, especially if its in Docker on Kubernetes, this might not be so easy. For ads.txt there are plugins, but for your other files? I just want to scp files from my computer to my site. Problem is, that often the web root is inside the container, not exposed as a volume (true for Bitnami's Wordpress image for instance where /opt/bitnami/wordpress exists just inside the container).

After looking for fixes with .htaccess etc. I finally decided on doing it with a custom 404 handler. In every theme, there's a 404.php that you can change. I changed mine to look into a folder that is available as a volume (/bitnami/wordpress). If the requested file is found there, it's streamed to the browser. If not, the regular 404 page is shown.

Here's the custom 404:

<?php

$potential_download = $_SERVER['REQUEST_URI'];
$path = "/bitnami/wordpress/wp-content" . $potential_download;
$allowed_extensions = array("txt", "mp3", "mp4", "mkv", "csv", "gif", "jpg", "png", "webp", "zip", "tgz");
$ext = pathinfo($path, PATHINFO_EXTENSION);

if (file_exists($path) && in_array($ext, $allowed_extensions)) {
	$file_name = basename($path);
	$finfo = finfo_open(FILEINFO_MIME_TYPE);
	$mime_type = finfo_file($finfo, $path);

	header("HTTP/1.1 200 OK");
	header("Content-Type: $mime_type");
	header('Content-Length: ' . filesize($path));

	$fp = fopen($path, 'rb');
	fpassthru($fp);
	exit();
} else {
	header("HTTP/1.1 404 Not Found");
	get_header(); ?>

	<div id="primary" class="site-content">
		<div id="content" role="main">

			<article id="post-0" class="post error404 no-results not-found">
				<header class="entry-header">
					<h1 class="entry-title"><?php _e( 'This is somewhat embarrassing, isn&rsquo;t it?', 'twentytwelve' ); ?></h1>
				</header>

				<div class="entry-content">
					<p><?php _e( 'It seems we can&rsquo;t find what you&rsquo;re looking for. Perhaps searching can help.', 'osirisguitar' ); ?></p>
					<?php get_search_form(); ?>
				</div><!-- .entry-content -->
			</article><!-- #post-0 -->

		</div><!-- #content -->
	</div><!-- #primary -->

	<?php get_footer();
}
?>

You don't even have to leave the comfort of your own admin page to do this. In the menu there's Appeareance -> Theme file editor where you can edit 404.php from inside Wordpress itself. If you are using Updraft or something similar for backup, this change will also be included in backups since it's in the theme. Beware though, it will be overwritten if you change it in a standard theme - make a child theme in that case.

Finally I can use this again to pester social media: https://www.osirisguitar.com/seal.png.

comments powered by Disqus
Find me on Mastodon