Improving SEO with Dynamic Content Using Server Side Includes and PHP

Improving SEO with Dynamic Content Using Server Side Includes and PHP

In case you have been living under a rock, SEO (Search Engine Optimization) is a very important topic for any website. Having fresh content is a powerful method of improving SEO, such as adding a list of the most recent blob post to your home page. With a few simple changes to your Apache Config files, you can use Server Side Includes to run PHP code snippets inside your standard HTML files. Why would I want to do that? My main website (http://www.thenextage.com) is a simple HTML website that I maintain with Dreamweaver. Instead of converting the site to PHP or WebDev, I wanted to keep the site simple and easy to maintain.  This article will explain how I am mixing PHP code into standard HTML files.

Feedburner has a great feature (BuzzBoost) that allows you to include a list of your blog post on the home page of your website, or anywhere else for that mater. I was using this to show the 5 most recent articles on my home page, as you can see in the screen shot below.

Feedburner makes this really simple, after setting a few configurations settings, they generate a JavaScript line for you to include on your site.

// <![CDATA[
src="http://feeds.feedburner.com/NextageConsulting?format=sigpro" type="text/javascript" >
// ]]>

Very easy, so what’s the problem? Although the JavaScript method is quick and easy it is not “Search Engine Friendly”. The content generated by the JavaScript is not seen by the search engine crawlers, all they see is the script code from above which they completely ignore.  So a JavaScript solution is out, if you want to improve your SEO!

I have another spot on my home page displaying upcoming technology events, in this article we are going to create a dynamic method to update that section, and keep it SEO friendly.

Unlike JavaScript, dynamic content  generated by PHP is “Search Engine Friendly”, since what the browser and the crawlers see is the actual content generated not the PHP code. Changing the whole site to PHP would be over kill for this website,  I want to keep this site in HTML for simplicity and maintainability sake, so including PHP code in my HTML files is the solution I want.

Before we get started a little technical information: my webserver is a virtual server hosted at rackspace with Centos Linux, Apache, PHP, and mySQL. Because I own the virtual server I have access to the Apache config files, etc. What follows is how I configured my system, if you don’t have access to the Config files you can accomplish this using .htaccess files. I am sure this can also be done on IIS but you will have to do some Google research to find the specifics.

We are going to use Server Side Includes (SSI). SSI is a simple technology that allows you to include external files inside another file.  Here’s an example include statement:

<!--#include virtual="../quote.txt" -->

However by default Apache will not process SSI, the statement will be treated as a comment and simply ignored. We have to change the Apache config file (or edit .htaccess file if you don’t have access to the config file). We need to add the “Options Includes” directive. This tell Apache that we are going to be using SSI, so it should look for them and process them. Below is the “” section of my config file. As you can see there are several different values available for Options so I just added Includes to the end.

    Options Indexes FollowSymLinks Includes
    AllowOverride All
    Order allow,deny
    Allow from all

At this point we have SSI working, but if we attempted to include a PHP file, the PHP code would just show on the website. We need to add  a couple more lines to the config file to let Apache know that it should process the files with PHP. If you do some Google searching you will find several articles telling you how to change your config file so that all HTML files get processed via PHP, or to rename all of your files with a PHP extension. Both of these methods will cause every requested page of your website to be processed via PHP, which will add extra burden to your web server. If you have a low volume site that might not be an issue, but its always best to learn to do it right the first time.

So we add two more lines to the Apache config file, as below:

Addhandler applications.x-httpd-php .html .php
XBitHack on

The AddHandler line tell Apache that we want to process the .html files via PHP. XBitHack tells Apache that we only want HTML files that have the Execute attribute set, to be processed via PHP. So now our normal HTML files will works as before, but if we set the Execute attribute on the file, then it will be processed via PHP. Since our index.html file is where we are going to add our dynamic content we turn on the Execute attribute with the following command:

chmod +x index.html

Now we write a simple PHP script to create our content. I keep the a simple table of the events in a mysql database on the server. All I do is maintain the table, and the PHP script handles show the next three upcoming events. Here’s the PHP script:

<!--?PHP
$db_handle = mysql_connect("localhost", "username", 'password');
$db_found = mysql_select_db("nextageevents", $db_handle);
if ($db_found) {
    $SQL = "SELECT * FROM events where enddate >= sysdate() order by startdate limit 3";
    $result = mysql_query($SQL);
    print "

“; print ”

“; } print ”

“; print ”

";
    mysql_close($db_handle);
}
else {
   mysql_close($db_handle);
}
?>

The first line connects to the MySQL server. You will have to change the username and password based on your setup of course. Next we connect to the specific database that has the events table. The we execute a SQL statement to get the next 3 upcoming events from the table. The print lines are the lines that are actually outputing the content. I am using CSS classes to format the content as I need via my site’s standard CSS file. And finally we close the connection to the database. We will save this PHP script as “events.php”

All that’s left to do is use the SSI syntax to include our new PHP script in the appropriate spot of our index.html page using the following line:

<!--#include virtual="/events.php" -->

And here is the screen shot of the results of our new home page:

Summary

With a few quick changes to our Apache config files, a simple PHP script and a MySQL database we have added some dynamic content to our home page, that will boost our SEO. There’s not maintenance of the HTML files, all we have to do is add records to the events table for any event we want include on the list. Now I am off to do the same thing for the Blog Post section and get rid of the FeedBurner JavaScript version that is not giving me any SEO boost.

2 thoughts on “Improving SEO with Dynamic Content Using Server Side Includes and PHP

  1. Hi,
    This is my first site I create http://tulistulis.com with my poor html knowledge.. 🙂 and it’s still far from finish.
    and I just think to make it’s easy to manage, after searching I found the solution is using SSI, I just try to use it and it’s work.
    Now, the title of the site, widget on the right, and the bottom of the site is using SSI. so, with this I easy to change the content and it’s will go to all others the page.
    after I think “Is my method SEO Friendly?”
    And What are your write here in you site give me the answer for it. Thank you very much.
    if you have a time, please see my site and give me more suggestion to improve it. Thank you.

    Like

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s