Do You Know What A Search Engine Friendly URL's Is?

0 replies
  • ARTICLES
  • |
Creating Search Engine Friendly URL's
Operating a successful web site demands smart search engine practices, and that includes the very URL's of your web documents. This article takes a look at how to create URLs that search engines relish. A friendly web site means more hits...and added success.

Here is the issue: many search engines reject URL's that include a question mark (?). The question mark means that dynamic content is being called and populated within the web page, usually before it is even displayed. A typical ?'ed URL looks like this:

example.com/page.php?id=about&section=me

Luckily, Apache supports the ForceType directive, which will help us create a more search engine friendly web site while fooling our web visitors. Yes, I said 'fooling'. Are you feeling a little naughty? What about mischievous? Read on.

Through the course of this tutorial, you will learn how to use the Apache ForceType directive, the PHP explode() function and the PHP PATH_INFO variable to contruct truly friendly web site URL's.

So, what is the solution to mean URL's?

The solution is twofold, and we will explore them both. Below, we will take a look at the ForceType directive, which allows us to define an extension-less document with a type, and how to use PHP to determine the latter portion of the URL.

THE APACHE FORCETYPE DIRECTIVE

Lets use the URL above as our example throughout this explanation. We will use the ForceType directive to remove the .php extension from the 'page.php' document. We are essentially fooling our visitors into thinking that 'page' is an actual directory, because our URL will eventually look like this:

example.com/page/about/me

We do this to expand security, albeit slightly. This particular security concept is known as "security by obscurity". We are obscuring the fact that our web site uses PHP (by removing the .php extension), which may detract PHP hackers from playing around with our web site.

We define our ForceType directive within our .htaccess document, which is a configuration file that all users have access to when using the Apache web server. If you have never used .htaccess before, be sure to name the file with a preceding period, then the file name, and without an extension.

Okay, lets see an example already. To remove the .php extension from our page.php document, while telling Apache to interpret the file as a PHP document, we use this code within our .htaccess file (placed in the same directory as the "page.php" page):

<files page>

ForceType application/x-httpd-php

</files>

This tells Apache that, every time the page "page" is called, interpret it as a PHP document. Next, we can manually rename the page.php document as "page". Now, your folder will include the file "page", without any extension, and an .htaccess file with the above code within it (plus any other files your folder may contain).

Believe it or not, that's it for our ForceType discussion. In the next portion of this article, we will see how to use PHP to parse through our URL, which will extract the information that we need (in this case, 'about' and 'me').

PHP's PATH_INFO VARIABLE

Now that we have seen how to remove the .php extension from our document, lets now look at how we code the page processing. PHP's built-in PATH_INFO variable holds the portion of the URL relative to the respective page. To illustrate, if PATH_INFO was placed within our "page" document, it would contain:

/about/me

Simply, it removes the document name and returns the URL to the right of it. Ultimately, we need to be able to access particular portions of the PATH_INFO directly. So, we need to extract the first part ('about') and the second part ('me'). How do we code this?

We use PHP's explode () function, which will split a string (in this case, "/about/me") into separate array elements based on a 'separator', or a 'delimiter'. Looking at our string, we can safely use the / as our separator text. And remember, the following code is placed within the "page" document.

The syntax for this looks something like the following:

$page = explode("/",$_SERVER['PATH_INFO']);

Here, we are creating an array called $page. We are assigning the elements of the array equal to the result we get AFTER the explode () function works. Our first parameter in the explode() function is our separator text (/); the second is our string that we wish to explode, or split.

And so, we can access the elements of our string with reference to our $page array. Let's take a look at what each array element, in our example, returns.

Array element Array value

$page[0]
$page[1] about
$page[2] me

Notice that $page[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. So, we will work with the second and third elements in our array element (blocks 1 and 2).

NOTE: If our string contained only /about, then $page[0] and $page[2] would not contain a value. If our string contained /about/me/and/you, then $page[0] would still be blank, but $page[3] and $page[4] would now contain 'and' and 'you', respectively.

We are almost finished. Armed with this data, we can now call the appropriate content to populate our page. If you call your content via SQL, then your SQL call might look something like this:

SELECT * FROM table WHERE cat='$page[1]' AND subcat='$page[2]'

Since $page[1] contains 'about' and $page[2] contains 'me', PHP will interpret that SQL call as:

SELECT * FROM table WHERE cat='about' AND subcat='me'

Some web sites include content via PHP's include() syntax rather than SQL calls. In this case, the syntax for achieving this is just as easy. Assuming that we have a directory called 'about' and a file called 'me.html':

$page[2] .= ".html";
if (include("/$page[1]/$page[2]")) {
// It worked!
} else {
include("/error_docs/notfound.html");
}

The first line of this code appends the .html extension to the $page[2] value, whatever it may be. This code assumes that ALL pages are named with a .html extension. The second line will look within the $page[1] ('about') directory for the $page[2] ('me.html') file, and will include it if it exists. If it doesn't exist, it will then include the "notfound.html" error document. This same effect can be achieved with PHP's file_exists () function.

Wrapping it up

And there you have it. We saw how to use Apache's Force Type directive to obscure our .php extension from our web site visitors. We then took a look at how to interpret our page and include the appropriate content using PHP's explode() function and PATH_INFO variable. The examples provided are just that, examples. Have fun with this concept and don't be afraid to play around with this to extend its functionality.

To Your Success,


J Mark
Home Biz Tips Newsletter
#engine #friendly #php #search #url’s #urls

Trending Topics