Use Markdown to deliver HTML pages through Apache

If you want to keep it simple one solution of delivering a website is Markdown. Markdown is a super simple to learn and use human-readable way of writing text that can also be understood by a parser. Most parsers allow creating HTML files, some also create other files like PDF.

Apache allows to define actions to perform on files before delivering them to the requesting client. So in order to create HTML pages from markdown you don't need fancy redirect chains or special configuration that parses everything "manually" through PHP for example. All you need is to edit your .htaccess file like so or add it to an existing file:

Action markdown path/to/parser/script.php
AddHandler markdown .md

With this you define an action called markdown and assign that action via file handler to all files with the suffix .md. The Action sends all requests to all .md files to the given parser script. In the script you have access to the translated path being the absolute file name of the file that was requested. All you now need to do is parsing the file with one of the many available Markdown-to-HTML parsers.

    require_once 'my-cool-parser.php';
    $parser = new MyCoolParser();


This example code outputs a fully valid HTML page. How to do this with the parser of your choice is entirely up to you. You might want to create the HTML <title> from the first top heading (# My Cool Heading):

$file_content = file_get_contents($_SERVER['PATH_TRANSLATED']);
$title_line = preg_split('#\r?\n#', $file_content, 0)[0];
$title = preg_replace('/^# (.*)$/', '\1', $title_line);

This gets the first heading and from that it gets the actual heading without the number sign in front. This could be used when creating the page. But It's fully up to you waht to do in your parser script. Apache simply does not care and delivers whatever your script returns.

Another benefit is that you can use Markdown files directly like HTML or PHP files. You just access them and Apache cares about the rest. You don't need to manually redirect all requests or do other fancy stuff. Just set up an action, assign it to .md files and things like or using instead of index.html are absolutely possible. (The later by defining DirectoryIndex index.html index.php in your .htaccess file).