Slash Work Mar28 '05

To follow up with my previous related entry, URI as <title>, today I delve deeper into URI manipulation, with the help of mod_rewrite and PHP.

The example

The requested page looks like this:

/specials/

You can obtain this string (for later use) by using the PHP variable $_SERVER['REQUEST_URI'].

You can then use this string to pull in the appropriate content, for the page. This can be done in many ways, but two common ways are mentioned below:

  1. Pull in a file called specials.php from your file system
  2. Write a database query with "specials" as part of your criteria

What about the slashes?

The slashes will certainly disrupt the process. After all, you can’t have a file named with a slash in it. It’s best to remove the slashes entirely, and just use the actual word.

With or without slash

Before we remove the slashes, let’s take a look at some good examples or user–friendly URI’s:

Notice how some of the URI’s have trailing slashes, and some do not.

URI’s that have a trailing slash indicate categories that could possibly contain further sub–items. If there is no trailing slash, that usually indicates the end of the line.

For example, /specials/daily/toothpaste/ contains a trailing slash, meaning it could possibly contain further sub–items.

On the other hand, /specials/daily/toothpaste/view-by-price does not have a trailing slash. This means that there is probably not any further sub–items after /view-by-price.

However, for usability purposes (as a user), it’s most appreciated when both methods call the correct page. In other words, the same page is presented, whether or not the trailing slash is included.

Remove the slashes

Again, your $_SERVER['REQUEST_URI'] variable contains the string /specials/, for this example.

Let’s set this long variable to something shorter, and easier to remember:

$page = $_SERVER['REQUEST_URI'];

We need to remove the slashes, so we end up with just the word specials.

First, the trailing (end) slash...

We can’t expect the user to add (or remember) the trailing slash. However, if the trailing slash is part of the request, we need to remove it:

if ( substr($page, -1, 1) == "/" )
{

$page_temp = substr_replace($page, '', -1, 1);
$string_reference = $page_temp;
}

If the trailing slash is not part of the request, we are already a step ahead.

The next step is to remove the initial slash – the one at the very beginning:

$page = substr_replace($string_reference, '', 0, 1);

Now, our variable $page will just contain specials, which is what we want.

Pull in the content

Now we can pull in the appropriate content, by using the string specials.

To include a physical file from your file system, try something like this:

$include_holder = @ include "includes/".$page.".php";

# CUSTOM 404 PAGE
if (!$include_holder)
{

echo "<h2>I’m sorry!</h2>\n";
echo "<p>The page you are requesting is not available, or does not exist.</p>\n";
}

So you’d have a page to correspond to each possible request. And, if a request doesn’t match any files in your file system, a custom 404 page is printed.

To make this more clear – a request for /specials/ would translate into specials.php, in the folder includes.

To pull your content from a database, instead of a flat file, try something like this:

$query = "SELECT field1 FROM table WHERE field1 = '".$page."'";

Deeper URIs

This works fine and dandy... except, what if the URI is a bit longer (or deeper)?

/specials/daily/toothpaste/crest/

Removing just the first and last slashes will not do the trick, in this case. We’d also need to remove the internal slashes, and possibly separate each word.

But how do we do that?

Rather than removing the internal slashes, we could just replace them with a more friendly character, such as an underscore (_), or dash (–):

$url_array = explode("/", $page);

$page_to_include = implode("_", $url_array).".php";

$page_to_include would then look like this:

_specials_daily_toothpaste_crest_.php

Alternatively, we could separate each word, to use as different parts in a query.

$url_array = explode("/", $page);

In this example, $url_array[1] would equal specials, $url_array[2] would equal daily, and so on.

The query could look something like this:

$query = "SELECT field1, field2 FROM table WHERE field1 = '".$url_array[1]."' AND field2 = '".$url_array[2]."'";

The bottom line

By manipulating the URI, with the help of mod_rewrite and PHP, we are able to create very user friendly web addresses. It also helps us to organize our content better (in FTP), by not having dozens of folders, nested inside other folders.

Next up...

My next entry on the topic will discuss the actual .htaccess file (mod_rewrite) that allows it all to work.

Categories: Apache , PHP

Add Feedback (view all)

Leave feedback

Feedback

Input format: The editor controls below will assist with Markdown syntax.

Status

Sub-status

Your info

matthom is published and produced by Matt Thommes - an independent publishing enthusiast, mobile blogger, content creator, informative writer, web developer from Chicago. Never one to conform, Matt intends to promote the effect the web has on our lives, in an effort to intensify, instruct, and clarify all that is happening around us.

Contact Matt

Similar Entries

Stats

4 unique visits since November 2008

Syndicate

Advertisements