Simple PHP alternative to a full CMS

The contents of this article may be out of date. It has been archived and will no longer be updated, comments are closed and the page is provided for reference purposes only. It is recommended you use a static site generator such as Jekyll.

Sometimes a when you build a website using a full content management system is not always an option. You may not want or be able to use a database or the clients budget might not stretch that far. This was the case in a recent project I undertook but I still wanted to use a single template.

It is very easy to use PHP Includes to separate content from design, you can just use a single line of PHP to include or require another file. The site I was developing had a static design, it had several pages, and the content for each page sat within the same area in the design. I wanted to be able to reuse my template and include the different content dynamically. This way if something was to be changed in the design it would require one change to the template, and not separate changes to all of the pages on the site.

With the design stage finished, a single template page is coded and named index.php with the following code at the top of the file:

  $file = $_GET[‘page’];
  if( isset($_GET[‘page’]) && file_exists(”/path/to/file/$file.html”) ) {
  $input = "/path/to/file/$file.html";
  } else {
  $input = "/path/to/file/main.html";

Within the coded template file you place the below code where you would like the content from the separate files to be placed:

<?php include(’$input’); ?>

When a page is called like so: or

The content from the contact.html page is placed where you have placed the include code from above, if the file does not exist, main.html is used instead (this should be your homepage content).

RewriteRule for the .htaccess file

This method uses the URL to pass a variable to the server. The problem with that is termed “messy URL‘s”. Search engines don’t rank messy URL‘s as well as clean URL‘s so ideally we want to use Apaches rewrite rule to clean up our messy URL‘s.

Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*)/(.*)/$ /index.php?$1=$2

The above code, when placed inside a .htaccess file in the same directory as your index.php file will change your hyperlinks from: to

These URLs are optimised for search engines, are a lot cleaner and easier for your visitors to understand.

Finally to prevent search engines and visitors accessing the page content outside the template you need to prevent access to the directory they are being stored in. Ideally the directory the content is stored in should be above the root directory but if this is not possible you can add the following to a .htaccess file to prevent the directory being accessed via a browser. Note this is a separate .htaccess file from the one in your root directory we have just used to rewrite our URL‘s. Create a new .htaccess file inside our includes directory and simply add this one line:

deny from all

.htaccess rules should have no effect on your script if it opens files in the denied directory directly; the .htaccess file is only used by Apache when serveing requests for a URL, and the script does not need to access it through the web server if it’s on the same machine.

This article was posted on 20 March 2009 in Code, PHP

That's the end of this article. I hope you found it useful. If you're enjoyed this article why don't you have a look around the archives, where you can find some more tutorials, tips and general ramblings.