Wildbug - I am an experienced PHP programmer but a complete noob when it comes to Regex, bare with me OK?
My content management system is well established, and has grown in capabilities and sophistication as I have learned new things. Because it is so well established, I need to keep the internally linked filenames the same, as I do not have time to go back and apply this new technique to every site. It will only apply to new sites. The new sites will have an .htaccess like this...
RewriteEngine On
RewriteRule ^(.*) filehandler.php?%{QUERY_STRING}
filehandler.php will then figure out what to do with the request by checking the $_SERVER['REQUEST_URI'] variable. .shtml files, for example, are prepared by my page editor module, so it would include the file that gets and renders those pages. Some preliminary tests using string hacking (substr, strpos et al) have proven the soundness of the basic theory. However, given my system contains about 2 dozen different modules, some with up to half a dozen rewrite rules, I do not want to have to write manual hacking code for each one. So I figured if I put the original expressions in the rewrite rules into a database, I could perform some kind of lookup. Obvious the expressions can be changed if need be.
I suppose MySQL regex and PCRE are both needed. Let's see if I can concoct a brief, simplified example code sequence of how I think it might/should work.
Say we have a database line with two fields. Expression and Destination Filename. Imagine the following example row.
Expression - "^links(.*)-(.*).html"
Destination - "index.html?page=links_manager/listings&Category_ID=#1&Start=#2"
Now the following code. I'll spell out where I do not what should happen, what functions to use etc. with caps and comments.
<?php
$Requested_File = "links1-1.html"; // this would ordinarily be derived from $_SERVER['REQUEST_URI'], simplifying here
$result = mysql_query("select * from rewrite_rules where Expression = REGEX COMPARISON TO $Requested_File HERE");
if ($row = mysql_fetch_array($result)
{
// code to get variables from $Requested_File by applying $row['Expression'] here
// preg_split probably the way to go
$Variable_Counter = 1;
$File_to_Include = $row['Destination'];
foreach($Preg_Split_Returned_Values as $Value)
{
$File_to_Include = str_replace("#{$Variable_Counter}", $Value, $File_to_Include);
$Variable_Counter++;
}
// now do something with $File_to_Include to present content
}
else
echo "File not found";
?>
My initial experiments with preg_split did not work. Looking at the documentation it does not look like the reg exs in the rewrite rules are directly compatible with the function. So they can of course be changed.
I hope this makes it clearer.