EDIT: Added in the header lines for 404 error, (
header("HTTP/1.0 404 Not Found");). In looking over the code, realized I forgot them. Without them, your site would never give a response to a invalid page, ie search engines would think it is a properly called page and index it
Basically you need to set ti to rewrite all requests that do not call an actual file/folder, to call a php script and pass it what you are trying to get
Here is something I normally use. The first condition forces the site to use www. version of the domain, the second block does the above:
[text]RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteRule ^(.*)$
http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*) /index.php?path=$1 [L][/text]
So now, if you go to
http://domain.com/gregs-page (assuming there is not a directory called "gregs-page") the following would actually be called:
http://www.domain.com/index.php?path=gregs-page
So now that you have this, index.php would take $_GET['path'] and look that up in a database, where you have the "SEO friendly" version mapped to the value you need (this is assuming that every call will be similar to the example you gave, each having a pid and id value (in this example, 404error is the page to display for a invalid URLs, index is the "home page")
[text]pathname (pk) | pid | id
--------------+-----+----
index | 1 | 1
gregs-page | 3 | 23
sample-stuff | 23 | 14
404error | 2 | 72[/text]
So you could have something like :
Code: Select all
<?php
// Sets up database connection
require_once('connect.php');
// Makes sure it was called correctly. Nothing passed we call 'index' something invalid, we call ''404error'
if (!isset($_GET['path'])) { $_GET['path'] = 'index'; }
elseif (!preg_match('/[.a-z0-9_-]+/i',$_GET['path'])) { $_GET['path'] = '404error'; }
$rsPage = mysql_query('SELECT `pid`,`id` FROM `tblPaths` WHERE `path` = "'.$_GET['path'].'"');
if (!rsPage || mysql_num_rows($rsPage) < 1) {
// The path given doesn't exist, force 404 error
header("HTTP/1.0 404 Not Found");
$rsPage = mysql_query('SELECT `pid`,`id` FROM `tblPaths` WHERE `path` = "404error"');
if (!rsPage || mysql_num_rows($rsPage) < 1) {
// At this point, we tried getting 404error page, but something still went wrong!
die ("The page you were looking for is not available");
}
}
$_GET = array_merge($_GET,mysql_fetch_assoc($rsPage));
mysql_free_result($rsPage);
unset($rsPage);
require_once ('page.php');
// EOF: index.php
If you are going to be using things other than page.php, and varying QS parameters, the you could instead do:
[text]pathname (pk) | module | qs
--------------+----------+---------------------------
index | page | pid=1&id=1
gregs-page | events | pid=3&id=23
sample-stuff | products | pid=23&producttype=4
404error | page | pid=2id=72[/text]
Then you can use the following:
Code: Select all
<?php
// Sets up database connection
require_once('connect.php');
// List of actual PHP files that are to be called by this system.
$aryModules = array('page','events','products');
// Makes sure it was called correctly. Nothing passed we call 'index' something invalid, we call ''404error'
if (!isset($_GET['path'])) { $_GET['path'] = 'index'; }
elseif (!preg_match('/[.a-z0-9_-]+/i',$_GET['path'])) { $_GET['path'] = '404error'; }
$rsPage = mysql_query('SELECT `module`,`qs` FROM `tblPaths` WHERE `path` = "'.$_GET['path'].'"');
if (!rsPage || mysql_num_rows($rsPage) < 1) {
// The path given doesn't exist, force 404 error
header("HTTP/1.0 404 Not Found");
$rsPage = mysql_query('SELECT `module`,`qs` FROM `tblPaths` WHERE `path` = "404error"');
if (!rsPage || mysql_num_rows($rsPage) < 1) {
// At this point, we tried getting 404error page, but something still went wrong!
die ("The page you were looking for is not available");
}
}
$aryLoad = mysql_fetch_assoc($rsPage);
mysql_free_result($rsPage);
unset($rsPage);
// This prevents any possibility of an bad data trying to call something not inteded.
if (!in_array($aryLoad['module'],$aryModules)) {
die ('Invalid call to site');
}
parse_str($aryLoad['qs'], $_GET);
require_once ($aryLoad['module'].'.php');
// EOF: index.php