PHP routing by mod_rewrite

Hi
This is my htaccess:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(js|ico|gif|jpg|css)$ /index.php [L]

if the file or directory with the specified name in the browser doesn’t exist then proceed to the rewrite rule

index.php

<?php
if(isset($_SERVER['PATH_INFO']))
$path = (substr($_SERVER['PATH_INFO'], -1) == '/') ? substr($_SERVER['PATH_INFO'], 0, -1) : $_SERVER['PATH_INFO'];
else
$path = (substr($_SERVER['REQUEST_URI'], -1) == '/') ? substr($_SERVER['REQUEST_URI'], 0, -1) : $_SERVER['REQUEST_URI'];
$url_parts = explode('/', substr($path, 1));
...
?>

Now each part of the address url put in url_parts array
How can I continue to build a Pretty URLs in php+pdo.
For example database with three table (settings - categories - posts).

Tables & fields:
Setting
{
id=1
title=englishsite
}

Categories
{
id=2
title=articles
}

Posts
{
id=15
title=subject
}

convert to:
http://mysite.com/englishsite/1/articles/2/subject/15
Or in other forms
how remove id numbers
http://mysite.com/englishsite/articles/subject

I’m NOT looking for a configuration of file .htaccess.
Need more information about php coding,
Thanks

Rather than exploding the path, instead match it against a regular expression.

if (preg_match('#^/([^/]+)/(\d+)/([^/]+)/(\d+)/([^/]+)/(\d+)$#', $path, $matches))
{
    // $matches[1] will be the setting title
    // $matches[2] will be the setting id
    // $matches[3] will be the category title
    // $matches[4] will be the category id
    // $matches[5] will be the post title
    // $matches[6] will be the post id
}
elseif (preg_match('#^/([^/]+)/([^/]+)/([^/]+)$#', $path, $matches))
{
    // $matches[1] will be the setting title
    // $matches[2] will be the category title
    // $matches[3] will be the post title
}

Thanks,
What are its advantages?

Now works in two modes, Only if 3 or 6 slashes in the URL and only responds for posts
What if we’re in the settings or categories url?
mysite.com/settings/1 ?
mysite.com/settings/1/categories/2 ?
preg_match here set for posts level only

if (preg_match('#^/(\d+)/([^/]+)/(\d+)/([^/]+)/(\d+)/([^/]+)$#', $path, $matches))
{
// $matches[1] will be the setting id
 // $matches[2] will be the setting title
// $matches[3] will be the category id
// $matches[4] will be the category title
// $matches[5] will be the post id
// $matches[6] will be the post title
$set_id = "$matches[1]";
$set_title = "$matches[2]";
$cat_id = "$matches[3]";
$cat_title = "$matches[4]";
$post_id = "$matches[5]";
$post_title = "$matches[6]";

if (isset($post_id)) {
include ('posts.php');
} elseif (isset($cat_id)) {
include ('categories.php');
} elseif (isset($set_id)) {
include ('settings.php');
}

}
elseif (preg_match('#^/(\d+)/(\d+)/(\d+)$#', $path, $matches))
{
// $matches[1] will be the setting id
// $matches[2] will be the category id
// $matches[3] will be the post id
$set_id = "$matches[1]";
$cat_id = "$matches[2]";
$post_id = "$matches[3]";

if (isset($post_id)) {
include('posts.php');
} elseif (isset($cat_id)) {
include('categories.php');
} elseif (isset($set_id)) {
include('settings.php');
}

}else{
include 'index.php';
}

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.