Utilizing URLs on your site that are search engine friendly is simple and easy to do thanks to PHP and Apache. We will be utilizing permalinks that get rid of all the nasty $_GET data that trails the end of most PHP scripts. An example of a SEO unfriendly URL we will clean is:
Using a combination of Apache’s ForceType directive, the PHP explode() function and PHP’s PATH_INFO variable we can easily turn the sample URL into:
This not only helps our website’s SEO (search engine optimization), but also accomplishes a security concept is known as “security by obscurity”. By obscuring the fact that our web site is using a PHP script, we may detract potential hackers from looking for exploits within our scripts.
Using Apache’s ForceType Directive
Apache supports the use of the ForceType directive, which will help us create a more search engine friendly web site. The ForceType directive allows us to define an extension-less document with a specific type of document. In this example we will be using the ForceType directive to remove the .php extension from the ‘article.php’ document.
The first thing you need to do is locate your home directory. This is often either your www directory or public_html directory. Locate your .htaccess file within and open it using any text editor. If you do not see this file you can simply create one using your text editor (such as Notepad on Windows).
To remove the .php extension from our article.php document, while telling Apache to interpret the file as a PHP document, we use the following code within our .htaccess file:
Some shared hosting environments such as those on GoDaddy or HostGator may need to use:
Note the “x-httpd-php5” which means it is using the Apache php5 module, as Apache 2 changes the way PHP is installed on some environments.
Be sure the .htaccess file is placed in the same directory that article.php is located. This tells Apache that every time /article/ is called to interpret it as a PHP document.
Open your “article.php” and save it without an extension as simply “article”. Your .htaccess file is now ready.
Using PHP To Make it All Work
Now that we have stripped the PHP extension from our document it is time to process the script. We will be using PHP’s explode() function along with the PATH_INFO variable to bring it all together. The goal is to use explode() to examine our PHP_INFO to determine our current URL and still have the script execute our $_GET commands, but now converted to array elements.
Using PHP’s built-in PATH_INFO variable it is possible to define the URL relative to the respective page. Simply put, it removes the URL up to the document name and returns only the trailing URL information to the right side. This will allow us to take our example URL and end up with only:
Using PHP’s explode() function we will split the string into separate array elements based on the delimiter/separator. Using our example string we can safely use the forward slash (/) as our separator text. The syntax would be:
$explode = explode("/",$_SERVER['PATH_INFO']);
Place the explode function in your document less PHP script near the top of the document before you execute any SQL query. Using this line we are creating an array called $explode and are assigning the elements of the array equal to the result we get after the explode() function is performed.
Our example $explode array will return:
Notice that $explode does not contain a value. This happens because of our first / in our string. The explode function will return the value BEFORE that first /. In this case, no text appears, so that element is blank.
We are almost finished. Armed with this data, we can now call the appropriate content to populate our page within our PHP script. If you call your content via SQL, then your SQL call might look something like this:
SELECT * FROM table WHERE id='$explode' AND title='$explode'
Since $explode contains “123” and $explode contains “seo-php-url”, PHP will interpret that SQL call as:
SELECT * FROM table WHERE id='123' AND title='seo-php-url'
Make sure your FTP program is set to upload as ASCII. Upload the document less PHP script “article” and the .htaccess file to your home directory (often public_html or www). Point your browser to a URL combination you know will return a valid result and marvel in your new SEO friendly site links.
We learned how to use Apache’s ForceType directive to obscure our .php extension from our URLs. How to interpret our pages address and include the appropriate content using PHP’s explode() function and PATH_INFO variable.