Creating Search Engine Friendly URLs With Apache and PHP

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:

http://www.myguysolutions.com/article.php?id=123&title=seo-php-url

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:

http://www.myguysolutions.com/article/123/seo-php-url

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:

<files article>
ForceType application/x-httpd-php
</files>

Some shared hosting environments such as those on GoDaddy or HostGator may need to use:

<files article>
ForceType application/x-httpd-php5
</files>

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:

/123/seo-php-url

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:

Array Element Array Value
$explode[0]
$explode[1] 123
$explode[2] seo-php-url

Notice that $explode[0] 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[1]' AND title='$explode[2]'

Since $explode[1] contains “123” and $explode[2] contains “seo-php-url”, PHP will interpret that SQL call as:

SELECT * FROM table WHERE id='123' AND title='seo-php-url'

Victory!

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.

12 thoughts on “Creating Search Engine Friendly URLs With Apache and PHP

  1. Pingback: designfloat.com

  2. Pingback: abcphp.com

  3. Many hosts (i.e. Bluehost) do not support $_SERVER[‘PATH_INFO’] – is it possible to do this with $_SERVER[‘REQUEST_URI’] instead? I tried simply replacing it in your code but the explode function doesn’t seem to work.

  4. Please make sure you are running PHP as an Apache module and not as a (fast) CGI application. The latter may not work.

    Otherwise – works like a charm. Thank you!

  5. I like your post.Many hosts (i.e. Bluehost) do not support $_SERVER[‘PATH_INFO’] – is it possible to do this with $_SERVER[‘REQUEST_URI’] instead? I tried simply replacing it in your code but the explode function doesn’t seem to work.

  6. Dear Sir,

    The code is not working on our Godaddy Shared Linux Hosted A/C. Please help us for the same.
    In case you want to check our site, please visit oshopindia and suggest us best possibility.

    Thanks
    Manthan
    Manager
    Oshopindia.com

  7. I like your article about the Forcetype for friendly url. I am new to this. I can it it to work following your example but when I try to implement it on my website I have problem. Let say the article (.php extensionless file) I place the normal html header and stuff and also use PHP to include external php files, that does not see to work. The include file not showing. How to deal with that?

    Thanks

  8. Unfortunately, all this comes undone if using multibyte characters, as while multibyte names work for real folders, so that a file inside them is accessed correctly via URL, or using glob to get the path, none of the S_SERVER variables return multibyte characters, but only ‘?’s instead, so that the path parts cannot be extracted properly for this technique to work.

    Pity that even old SMS have got with the multibyte program, but parts of URLs, like GET variables, are still in the stone age. Even links (#id) targets in pages can be multibyte!

  9. Apple’s flagship operating system iOS 11 is nearing it’s release. There will be quite a few changes from iOS 10 as well as some compatibility updates. You can check out my site to get all the details about iOS 11, the compatible devices, release date and
    iOS 11 download

Leave a Reply

Your email address will not be published. Required fields are marked *