making clean urls using PHP ??

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

It does work, you just have to set up your own url mapping scheme.

For example if you had the url http://www.example.com/games/play/37/some-game

Code: Select all

$parts = explode('/', $_SERVER['PATH_INFO']);
Now you have every part of your URL into an array. Which you could then map to games.php?action=play&gameid=37

You don't need to have the .php extension in your URL, and you don't need to use .htaccess.

Let's choose a much simpler URL /articles/1357

Code: Select all

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

//check if the first part exists.. we can add the .php on here
if (file_exists($parts[0] . '.php'))
{
     $str = 'articles.php';
     
     //do some querying or something to check if $part[1] (1357 in the example, is a valid article id
    if ($is_valid_article_id)
    {
        $str .= '?id=' . $parts[1];

        //redirect to page, or include() page, or whatever
        header('Location: http://www.example.com/' . $str);
        exit;
    } else
    {
        header('Location: http://www.example.com/articles.php');
        exit;
    }
} else
{
    header('Location: http://www.example.com');
    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.
User avatar
PHPycho
Forum Contributor
Posts: 336
Joined: Fri Jan 06, 2006 12:37 pm

Post by PHPycho »

nope that works .......
Post Reply