Choose Wisely: PHP Templates and Search Engine Rank
September 23, 2022
This article is intended for PHP web developers who create template systems for consistently presenting website content. PHP designers who create their own template systems should be concerned about how their choices affect the website’s position in search engines. Below is a discussion of three types of systems, their impact on search ranking and a recommendation for the best approach.
Webpage as a CGI Argument
In this architecture one PHP script contains the entire template, and plugs in content from a file it reads in – getting the file name from a CGI parameter. These types of systems are not only prone to security problems (as users can modify the path argument to retrieve arbitrary files) but are also ineffective for search engine optimization. This particular design does not allow for titles and keyword/description meta-tags to be associated with individual pages. This is because the header portion of the template includes the section within it, and only allows for one site-wide title and set of meta-tags. Titles are one of the most important page elements, and having one title – unrelated to the actual content retrieved – results in a lower rank in search engines.
There are two other detrimental SEO effects this type of system has. The first is that each unique page is viewed as an aspect of the template script, rather than as a unique standalone page. This can result in search engines lowering the importance of individual content pages relative to the template script – which is often not the desired result. The other weakness of this approach is that having individual pages set up as CGI arguments makes site metrics harder to track, as statistics often show a visit to a specific page as a visit to the general template script. The popular web statistics packages AWstats and Webalizer are most affected by this.
However, this type of templating system is popular and successfully used in the world of e-commerce. In such systems, product information is stored in a database and is inserted into the template when a product is requested by its code. This means that both a unique title and a set of meta-tags can be stored alongside the product data (or generated on the fly), and inserted into the global template effectively. The only detrimental effects that remain are the latter mentioned two, and are usually an acceptable tradeoff for the increased ease of administration that a database-driven system provides.
Template as an Include
In this architecture, each page on the website has its own filename with a .php extension. The top and bottom portions of the template are stored in separate files that are included (using include_once() ) by each content file. This technique increases the importance of the page with search engines, as each page is its own standalone page rather than a CGI argument. This type of template system also makes it simple to track page views through regular website statistics software.
On the other hand, this type of template system still has the weakness of having one title and one set of meta-tags for the entire site. This is, again, because the header portion of templates has to include everything from the opening of the tag to the end of the code for the header portion of the template. And this area includes the title and meta-tags. One workaround for this type of system is to code the top template page to check which file it is being called from, and to substitute a suitable pre-coded title. Although effective, such a system is cumbersome to maintain as each new page that is published warrants a new title and meta-tag entry in this secondary system.
The ideal PHP templating system will permit content producers to enjoy the reusability of templates, while maintaining unique titles and metadata for each page with ease. The system that meets all of these SEO needs is a “body-only include” system: a system where each php content page includes the top and bottom portions of the template (as in the previous approach), but only includes the elements below the body tag. This translates into a document that has its own html, head, title and meta tags, has an include_once() statement which is called at the top of the body element and includes the top portion of the template, has the content, and an include for the footer portion of the template. Such a system allows for individual editing of the title and meta-information during the creation of documents and solves the problems that both of the aforementioned systems face.
I find this system the most search-engine friendly, because it keeps the title information and the content together – allowing for the presence of relevant, unique titles for each page. This system is also the most human friendly, as storing the title information in the same file as the page content allows for fast updating and avoids maintaining a second “workaround” system that attempts to match titles to content.
The types of template system that PHP developers choose to implement for clients’ sites have a crucial impact on the future search-engine popularity of the client’s webpages. Important factors to consider when developing a template solution are: relevant titles tied to content, relevant meta-tags, standalone files and ease of statistics tracking. The popular approaches of page-as-an-argument and template-as-an-include do not effectively provide these functions. Including template information after the body tag, and keeping headers intact is the best way to ensure that sites are optimized for search engines – and this is why you should consider using the Body-Only Includes model during your next project.