Dynamic websites are essential for content heavy sites and chances are, you’re not going to create one static page for every single page/article/product item you have stored in a database. This means you’re going to need to pass parameters to your script to pull the right content from your database. Up until recently, most people didn’t give much thought to those nasty URLs with all those ampersands (&) and equal signs and how they affect spidering by search engines. While these URLs are perfectly valid, they tend not to get indexed by Google (and other search engines) unless you submit each URL for indexing (which can cost you money). And these days, if you’re not in Google, you’re not being found.
Enter PHP and Apache
<p><p>If you’re using <span class="caps">PHP</span> and Apache, you can still be dynamic and be spidered by all the popular search engines fairly easily. Of course, this depends on your hosting provider and how much control they let you have in terms of htaccess. <span style="font-style: italic;">.htaccess </span>files let you manipulate some Apache settings on a per host/per folder basis. Have a look at the <a href="http://httpd.apache.org/docs/">Apache manual</a> for a full listing of configuration settings for Apache web server.</p></p>
<p><p>Now for the goods. Here’s a basic rundown of how search engine friendly URLs can be used in a dynamic way. Let’s say you have a catalog of items, and your script takes 3 parameters: ‘section’ , ‘action’, and ‘item.’ Normally, when passing parameters to a script, your url will look something like this:</p></p>
<p><pre><code>/index.php?section=widgets&action=view&amp;item=327</code></pre></p>
<p><p>With the proper directives in an Apache .htaccess file and some simple <span class="caps">PHP</span> scripting, you can turn that <span class="caps">URL</span> into something like this:</p></p>
<p><pre><code>/catalog/section/widgets/action/view/item/327</code></pre></p>
<p><p>Looks like a bunch of folders, but it’s not. ‘catalog’ is actually a file (a <span class="caps">PHP</span> file) without the extention, and with the help of a <span style="font-style: italic;">.htaccess</span> file, Apache treats it like a <span class="caps">PHP</span> script. Here’s what the <span style="font-style: italic;">.htaccess</span> file looks like:</p></p>
ForceType application/x-httpd-php
<p><p>That’s it. Nothing too complicated. Now for the trickier part, grabbing those parameters from the <span class="caps">URL</span> with <span class="caps">PHP</span>.</p></p>
<p><p>There’s a server variable that gets passed to the script calle ‘PATH_INFO’ which contains the entire string after the catalog file (including that first slash). To grab it in a <span class="caps">PHP</span> script, you’d use the <a href="http://us2.php.net/manual/en/reserved.variables.php#reserved.variables.server">$_SERVER</a> global variable (a la $_SERVER[‘PATH_INFO’]). The idea is to <a href="http://us2.php.net/manual/en/function.explode.php">explode</a> the string into an array and loop through them to get the parameters. Here’s a small function to do so:</p></p>
<p><pre><code>< ?php
function getArgs()
{
$params = explode(”/”, $_SERVER['PATH_INFO']);
}
?>
<p><pre><code>for($i = 1; $i < sizeof($params); $i = $i + 2)
{
$args[$params[$i]] = $params[$i+1];
}
return $args;
<p><p>When used on the above example, this function will return an array like so:</p></p>
<p><pre><code>Array (
[section] => widgets
[action] => view
[item] => 327
)
<p><p>Implimenting this into a current site shouldn’t be too hard. The ‘catalog’ script would simply be a wraper script where you can pass the required values into existing code.</p></p>
<p><p>If you find that your web host doesn’t allow use of .htaccess files, or they don’t allow the <Files> directive to be used, you can do one of two things (and I highly recommend the first).</p></p>
<p><p>1) Change hosts! I use <a href="http://www.dreamhost.com/rewards.cgi?sesser">Dreamhost</a> and they are extremely flexible, have an awsome support team and have a <a href="http://www.dreamhost.com/rewards.cgi?sesser/shared/comparison.html">very good deal for $9.95/mo</a>.</p></p>
<p><p>2) Just use a normal <span class="caps">PHP</span> script (i.e. catalog.php) and use the same function. While this method works, I’ve read that some search engines see this as a hack/cheat and might not spider the pages anyway. However, if you are a reputable business, you can usually contact them and get your pages indexed.</p></p>
Other Web Servers
<p>If you’re running <span class="caps">IIS</span> (god forbid), there is hope for you. There is at least one product (ISAPI) available that mimics the Apache mod_rewrite module. This <span class="caps">ISAPI</span> can help do the same thing (although it won’t be quite as easy). But the drawbacks are that a) the <span class="caps">ISAPI</span> isn’t free and b) if you’re in a shared environment, you can’t use it. There are other ways to manipulate your <span class="caps">IIS</span> settings to use search engine friendly URLs, but they all have their downsides. Bottom line: stop using <span class="caps">IIS</span>.</p>
<p>As for other servers, I can’t really say. Chances are there are some tweaks that can be made to the configuration and/or scripts. If you know of any, please drop me a line and I’ll try to get it posted.</p>