<pre>
<?php
/**
* parseModRewrite() Function
* Created by: Joshua H. (TRUSTAbyss)
*
* Usage: parseModRewrite("HTACCESS FILE") will output a multi-dimensional array
* that can be used to convert to Abyss's URL Rewriting.
*/
function parseModRewrite($file)
{
// Check to see if the file exists before proceeding.
{
// Initialize the arrays and the $base variable (used for other rules).
$base = '';
// Open the htaccess file to be parsed.
$file_array =
file($file);
foreach ($file_array as $lineNum => $line)
{
// Remove whitespace from the beginning and end of $line.
// Replace whitespace inside RewriteCond/RewriteRule with \s character.
{
}
// Read the current line and replace multiple whitespaces with a single space.
// Check for a RewriteBase.
if (preg_match("/^RewriteBase\s(\/[^\s]*)$/i",
$line,
$matches)) {
// Create the variable for RewriteBase ($matches[1]) and store the permanent.
$array['base'] = $matches[1];
$base = $matches[1];
}
// Check for a RewriteCond.
if (preg_match("/^RewriteCond\s([^\s]+)\s([^\s]+)(\s\[([^\s]+)\])?$/i",
$line,
$matches)) {
// Create the variables for Variable ($matches[1]), Expression ($matches[2]), and Flag ($matches[4]).
$array['cond']['var'][] = $matches[1];
$array['cond']['exp'][] = $matches[2];
$array['cond']['fla'][] =
isset($matches[4]) ?
$matches[4] :
0;
// Gather info about the current line (not required).
$array['cond']['info'][] = 'Line '.($lineNum + 1).': '.$matches[0];
}
// Check for a RewriteRule.
if (preg_match("/^RewriteRule\s([^\s]+)\s([^\s]+)(\s\[([^\s]+)\])?$/i",
$line,
$matches)) {
// Ignore the Apache specific RewriteRule that fixes the Internal Dummy Connections.
if ($matches[2] == '-')
{
// Delete the conditions that belong to this RewriteRule.
if (isset($array['cond'])) {
}
// Continue to the next RewriteRule.
continue;
}
// Create the variables for Regexp ($matches[1]), Replacement ($matches[2]), and Flag ($matches[4]).
$array['rule']['reg'] = $matches[1];
$array['rule']['rep'] = $matches[2];
$array['rule']['fla'] =
isset($matches[4]) ?
$matches[4] :
0;
// Gather info about the current line (not required).
$array['rule']['info'] = 'Line '.($lineNum + 1).': '.$matches[0];
// If needed, create a RewriteBase path based on where the htaccess file is located.
{
// Create the variable for RewriteBase and store the permanent.
$base = $array['base'];
}
// Sort the rewrites in alphabetical order (base, cond, rule)
// Since we came to a RewriteRule, we can add the rewrite.
$rewrite[] = $array;
// We need to delete what was already in the array to add another rewrite.
// Loop through all the rewrites and change the RewriteBase if needed.
foreach ($rewrite as $group)
{
if (!
empty($group['base'])) {
$array['base'] = $base;
}
}
}
}
// Return the array or FALSE if it could not be parsed.
return (!
empty($rewrite)) ?
$rewrite :
FALSE;
}
else // The file does not exist.
{
return FALSE; // The function must return FALSE.
}
}
// Here's an example on using this function.
if ($parsed = parseModRewrite("drive:/path/to/.htaccess"))
{
}
?>
</pre>