0

Basically what the title says. I'm working on a project that does not involve classes and I would like to implement htaccess and mod rewrite to create pretty url's. Here are a few examples of what I want to be able to route. My file and directory structure is laid out below as well.

/root
     - index.php (bootstrap)
     /template
     /sources
          /forums
               -threads.source.php
               -discussions.source.php
               -post.source.php
               -index.source.php
          //
          - members.source.php (functions for register, login, etc)
          - index.source.php

http://localhost/myapp/members/register -> sources/members.source.php register function
http://localhost/myapp/ -> sources/index.source.php
http://localhost/myapp/forums/threads/Some-Cool-Thread/0001 -> sources/forums/threads.source.php view function
http://localhost/myapp/forums/ ->sources/forums/index.source.php

I hope this makes sense to you. By the way: I am using .source.php as file names because I will also have template files that will use, for example, members.template.php

EDIT: I have already made my .htaccess file and have also gotten the URI string in my index file. I have a bit that validates the uri segment characters. Here's my .htaccess file:

Options +FollowSymLinks  
    RewriteEngine On  
    RewriteCond %{SCRIPT_FILENAME} !-d  
    RewriteCond %{SCRIPT_FILENAME} !-f  
    RewriteRule ^(.*?) index.php

Here is my index file:

<?php

session_start();

require_once('config.php');

global $config;

date_default_timezone_set($config['server']['default_timezone']);

define('DOC_ROOT', dirname(__FILE__));

if(!mysql_connect($config['db']['server'], $config['db']['user'], $config['db']['pass'])){

    die('System Failure:  Could not connect to the requested database server.  It may be down or it may be too busy!  Please check back later.');

} else if(!mysql_select_db($config['db']['name'])){

    die('System Failure:  Could not connect to the requested database server.  It may be down or it may be too busy!  Please check back later.');

} else {

    $uri_segments = $_SERVER['REQUEST_URI'];

    $uri_segments = str_replace('/Base%20Command/', '', $uri_segments);
    $uri_segments = explode('/', $uri_segments);


    foreach($uri_segments as $segment){

        if(!preg_match('^[a-z0-9_-]^', $segment)){

            die('Could not accept provied URI string... the string contains invalid characters.');

        }

    }

    if(file_exists(DOC_ROOT.'/sources/global.source.php')){

        require_once(DOC_ROOT.'/sources/global.source.php');

    }

    if(file_exists(DOC_ROOT.'/template/global.template.php')){

        require_once(DOC_ROOT.'/template/global.template.php');

        if(function_exists('html_header')){

            $output = html_header();

            if(file_exists(DOC_ROOT.'/template/'.$source.'.template.php')){

                require_once(DOC_ROOT.'/template/'.$source.'.template.php');

                if(function_exists($action)){

                    $output .= $action();

                } else {

                    $output .= Error404();

                }

            } else {

                $output .= Error404();

            }

            if(function_exists('html_footer')){

                $output .= html_footer();

            }

        } else {

            $output = 'System Failure:  Certain template files did not load correctly.';

        }

        echo $output;

    }

}

?>

Basically what I need to do is to find a way to efficiently come up with values for the source and action variables.

ShoeLace1291
  • 4,551
  • 12
  • 45
  • 81
  • 2
    Totally possible, but this is a very wide-ranging question. Could you perhaps narrow it down a bit? Have you tried playing with mod_rewrite yet? Maybe you should, and then should ask for more specific help when you encounter problems. – Charles Dec 27 '12 at 07:43
  • I've updated my original question. – ShoeLace1291 Dec 28 '12 at 04:05

0 Answers0