Creating Search Engine Friendly URLs In PHP
One of the major reasons for using a server-side language such as PHP is for the ability to generate dynamic content. Often this will lead to single scripts that produce their content based on the input parameters (that is, the variables in the URL).
This article covers various techniques and methods for representing these parameters in the URL in a clean and “friendly” manner, as well as then how to read the parameters.
If you’re not quite sure what I mean, take the following example. This website (PhpRiot) stores each of its articles in a database table called
articles. Now, we could have built the site so all articles were referenced by their article ID, such as:
However, this isn’t necessarily the best way to do it. Firstly, if people have read a large number of articles on the web site, then their URL history will contain a whole bunch of different IDs, so they won’t be able to directly back to an article without either bookmarking it or visiting the home page and finding the link.
More importantly though, this is wasting valuable data that the search engines can use in indexing your site. PhpRiot has been built in such as way that each articles are accessed in a more human-readable format. For example, a previous article on this site is accessed with the following URL:
This article will go over how read URLs like this and map them back to the data in your database. There are several methods that can be used with PHP, so we will cover each of these and discuss the pros and cons of each.
Additionally, the ideas used in these may cross-over between each method (such as the manual parsing of URLs), but we will also go over this.