This fit my needs perfectly and is very simple and allows for a great amount of flexibility. This will allow me to write any amount of pretty url's with unlimited depth while maintaining a single directory structure for my files.
Example URL: example.com/foo-bar/ would use /foo-bar.php
Example URL: example.com/foo-bar/string/ would use /foo-bar_string.php
Example URL: example.com/foo-bar/string/subsection would use /foo-bar_string_subsection.php
Example URL: example.com/a/b/c/d/e/f/g/h/i would use /a_b_c_d_e_f_g_h_i.php
This file should be used as index.php.
The following .htaccess rules should be used (thanks to requinix @ forums.devnetwork.net)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*) index.php [L]
$_GET and $_POST will still be abailable in their usual manner
<?php
/**
* A very simple URL manager/rewriter/router, set this file as index.php
* With .htaccess rule, allows any length of "pretty URL's" to be written and
* funneled to this index.php and routed to their actual scripts.
*
* For example, a URL of /foo-bar/ would be directed to /foo-bar.php
* For example, a URL of /foo-bar/example/ would be directed to
* /foo-bar_example.php
* For example a URL of /foo-bar/example/detail/subsection/ would be directed
* to /foo-bar_example_detail_subscection.php
*
* The .htacess file should look like this: (thanks to requinix @ forums.devnetwork.net)
*
* RewriteEngine On
* RewriteCond %{REQUEST_FILENAME} !-f
* RewriteCond %{REQUEST_FILENAME} !-d
* RewriteRule ^/?(.*) index.php [L]
*
* @file index.php
* @author Scott Martin <sjm.dev1 -[at]-gmail-[dot]- com>
* @date Feb 13th, 2013
* @package None
*/
if (!empty($_SERVER['REQUEST_URI']))
{
//to avoid recursion, actual index content must be in separate script
if ($_SERVER['REQUEST_URI'] == '/' || $_SERVER['REQUEST_URI'] == '/index')
{
require_once 'index_content.php';
exit;
}
//request uri will be available and stored in $fileName
$fileName = $_SERVER['REQUEST_URI'];
//get rid of the query string if present (it is still available in $_GET)
if (strpos($fileName, '?') !== false)
{
$fileName = strtok($fileName, '?');
}
//replace beginning and trailing slashes
$fileName = trim($fileName, '/');
//replace slashes with underscores, and lowercase
$fileName = strtolower(str_replace('/', '_', $fileName));
//check for appropriate characters and file exists
if (preg_match('#^[a-zA-Z0-9_-]+$#', $fileName) && file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $fileName . '.php'))
{
require_once $_SERVER['DOCUMENT_ROOT'] . '/' . $fileName . '.php';
exit;
}
}
//if we reach this point, we send a 404 header
header('HTTP/1.0 404 Not Found');
require_once '404.php';
exit;
_________________
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.