I design every site in PHP, even if there isn’t a database involved. Why? It actually saves me time and my clients money. By creating a ‘template’ page called index.php and dynamically inserting the page title, meta tags, menu, content, and anything else that changes on a page-by-page basis, a website can be much more dynamic and easier to update and maintain.
The advantages of this technique are huge – if a client wants to add a page to the menu, it only takes a minute, instead of hours (if you are dealing with a large website). Sure there’s always search & replace functions, but that will inevitably lead to mistakes & inconsistency.
So What’s the Downside?
The only downside of this technique is that some people don’t like or understand the query strings in the url that result from this method. For example the URL for Jellyflea Web Design’s portfolio page is:
http://www.jellyflea.com/index.php?p=portfolio
Now, being a web designer, that doesn’t bother me in the least, but some clients prefer to see something more familiar like:
http://www.jellyflea.com/portfolio.html
Mod_Rewrite to the Rescue!
Luckily, thanks to Apache and mod_rewrite you don’t have to abandon the dynamic page generation. You can include an .htaccess file that looks something like this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.jellyflea\.com$ [NC]
RewriteRule ^(.*)$ http://www.jellyflea.com/$1 [L,R=301]
RewriteRule ^(.*)\.html$ /index.php?&p=$1 [L]
Now when someone goes to http://www.jellyflea.com/portfolio.html they get redirected without ever knowing it.
Convert Existing Links from Dynamic to Static
Sometimes the client will request this after the design is done, which forces you to update all your links to match the new html format. Luckily, you can do a search and replace function in your regular expression equipped text editor. Here’s instructions on how to replace all instances of links with the new format by using Smultron.
- Open all your files as a project.
- Go to Edit->Find->Advanced Find and Replace
- Choose “Current Project”
- Find: index.php\?p=([\w-$]+)
- Replace: $1\.html
- Click “Replace”
… and index.php?p=page-name is replaced with page-name.html.