Have you ever had to deal with a sloppy site? Maybe it was when you were working with a client. Or could it be your own site, which you formatted poorly because of inexperience? Regardless, there is a simple yet efficient way to make your site based completely off a template. All it requires is a bit of htaccess and some PHP.

Time for some incentive

This will be a multi-part tutorial in which we explain how to completely transform your site into an organized beast. Today’s section will focus on a few lines of .htaccess that you will soon come to love. But, before we begin, let’s go over a few advantages and some incentive for creating a template-focused site.

  1. Maintained through a simple template that regulates code and prevents reuse. This enables you to focus on content rather than code.
  2. Easily editable by changing any part of the template. Doing so will immediately change all pages of a site. There is no need to go into multiple files.
  3. Can simply be hooked up with a MySQL database for even further organization.
  4. Adding pages that jive with your site is as simple as making a file with some text (not HTML/PHP–just content)
  5. Can easily be created to have pretty URLs without having to delve deep into .htaccess.

The above list just constitutes a small number of reasons to switch to a template-focused site. A quick note for all you CMS users (WordPress/Joomla/Drupal/etc.): your site is already optimized with a template! This tutorial is mostly referring to those who are creating or have a site that is not running on a CMS.

Bring on the htaccess!

Do you need to be a htaccess genius to create a template-focused site? Of course not! Take a look at the following code. This is all you’ll need:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?cur=$1 [L,QSA]

It may look a bit complicated at first, but it is actually quite simple. This all starts with a request to your site (such as "http://your-site.com/something.html"). If the given request is not a file nor a directory, it will redirect the request to index.php.

As an example, if the user requested "http://your-site.com/asdf", which is neither a file nor a directory, it will redirect to "http://your-site.com/index.php?cur=asdf". The great part about this is that the URL will stay the same. The user will still see "http://your-site.com/asdf" in their URL bar, but index.php will actually be handling the request. Not only does this give you great flexibility, but it makes the management of failed requests (such as not found, forbidden, etc.) extremely simple.

How about that PHP?

Consider the following index.php:

<?php
	$request = isset( $_GET[ 'cur' ] ) ? $_GET[ 'cur' ] : 'home';

	$pathToFile = dirname( __FILE__ ) . '/pages/';
	$fullPath = $pathToFile . $request . '.php';

	if( file_exists( $fullPath ) )
		require_once $fullPath;
	else
		require_once $pathToFile . '404.php';
?>

This is used to parse the request from the user. It first finds the requested page by getting the cur parameter passed to index.php (which is a GET request). If this doesn’t exist, it defaults to the home page. Next, it finds the corresponding PHP page under the pages/ directory and then includes it. So, for example, let’s say you had the following setup:

pages/
.htaccess
index.php

And in the pages/ directory, you have:

404.php
home.php
page.php

If cur is not set, index.php will display home.php in the pages/ directory. Otherwise, it’ll display [cur].php in the pages/ directory, where [cur] is the value of cur.

Sample Occurrences

So, let’s trace a few requests for this site. If the user goes to "http://your-site.com", the htaccess will redirect the user to "http://your-site.com/index.php?cur=".

Next, index.php will receive the request. It will realize cur is not explicitly defined, so it will set it to “home”. After that, it will find the full path to home.php in the pages directory and make sure that the file exists. Because this file does exist, it will include it into the page and the contents will be displayed.

How about "http://your-site.com/page"? This will be redirected to "http://your-site.com/index.php?cur=page".

Since cur is set, index.php will find the corresponding file (which is page.php) inside the pages directory. Once again, because this file exists, it will be included into the page.

Let’s do one last example. Consider "http://your-site.com/test". This will redirect internally to "http://your-site.com/index.php?cur=test".

Cur is explicitly defined. Thus, index.php will look for test.php in the pages/ directory. Unfortunately, this file does not exist. Thus, index.php will serve up 404.php in the pages/ directory. This file displays information that tells the user a 404 (not found) error occurred.

To see this all in action, you may view a demo of the site setup by clicking the link below. Please note that there is no real content on the site – it is just an example that tells you what page is being delivered. Consider requesting “/”, “/page”, and “/test” to see the home.php, page.php, and 404.php in action, respectively.

As you can see, with just a little amount of code, the effects of templating can be great. This, of course, is just the starting line of a template-focused site. There is still a lot more to cover. Join us in the next installment when we expand on this concept and teach you how to become even more organized.

If you liked this post, please leave an encouraging comment below! It really helps us keep Lateral Code up and running.

Leave a Reply

Need more organization? Make a template-focused site! – Part 1