How this website works internally
This Website is powered by a little PHP script I wrote. It utilizes a very tiny flat file CMS: less than 20 lines of PHP code and less than 20 lines of HTML. Most stuff is done by PHP Markdown by Michel Fortin for formatting the page content.
No database system - just plain text files. Very fast and almost unbreakable.
The basic idea is:
Let the apache web server redirect the request to this PHP script with options based on the request string after the domain without the trailing ".html" through
.htaccess
.The PHP script looks for an
.md
file with the requested name. This file contains in the first line the page title following standard markdown code.If it exists the "PHP Markdown" engine is feeded with its content or with the content on a predefined error page otherwise-.
The result will be injected in the website template.
Example:
request is:
http://www.example.com/test.html
through
.htaccess
rules the resulting request is:http://www.example.com/index.php?page=test
this PHP script looks for
test.md
if it exists the "PHP Markdown" engine is feeded with its content
the result will be injected in the website template
So in detail:
.htaccess
:
RewriteEngine On
# everything before .html becomes option value $1
RewriteRule ^(.*)\.html$ index.php?page=$1 [L]
# the predefined error page error404.md
ErrorDocument 404 index.php?page=error404
test.md
:
page title
## Header 2
paragraph
index.php
:
<?php
require_once("Michelf/MarkdownExtra.inc.php");
$page = "index.md";
$title = "";
if (!empty($_GET)) {
$page = $_GET['page'] . ".md";
}
if (file_exists($page)) {
// read all lines of file in array $data of lines
$data = file($page);
// the first element (line) is the title (excluding PHP_EOL)
$title = array_splice($data,0,1)[0];
// the remaining data is the content
$text = implode(PHP_EOL,$data);
// pass content through the Markdown parser
$html = MarkdownExtra::defaultTransform($text);
}
else if ($page!=="index.md") {
header($_SERVER['SERVER_PROTOCOL']." 404 Not Found");
$title = "Error 404: Not Found";
$text = implode(PHP_EOL,array_splice(file('error404.md'),1));
$html = MarkdownExtra::defaultTransform($text);
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title><?php echo $title; ?></title>
</head>
<body>
<article>
<?php
echo $html;
?>
</article>
<footer>
© 2015 by Example
</footer>
</body>
</html>
Result:
http://www.example.com/test.htmlEXAMPLE
page title
Header 2
paragraph
You got the basic idea ...
Of course the actual website contains a bit more code e.g for error handling and syntax highlighting.