Created
April 2, 2015 17:50
-
-
Save aenglander/8e2f83c4526fccdcdead to your computer and use it in GitHub Desktop.
PHP .htaccess router for built in web server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
if (file_exists($_SERVER['SCRIPT_FILENAME'])) { | |
return false; | |
} | |
$_SERVER['DOCUMENT_ROOT'] = empty($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['PWD'] . '/www' : $_SERVER['DOCUMENT_ROOT']; | |
$htaccess = $_SERVER['DOCUMENT_ROOT'] . '/.htaccess'; | |
$rewrites = array(); | |
if (file_exists($htaccess)) { | |
$fp = fopen($htaccess, "r"); | |
while ($line = fgets($fp)) { | |
if (preg_match('/RewriteRule\s+(?P<expr>[^\s]+)\s+(?P<file>[^\s]+)/', $line, $matches)) { | |
$rewrites[] = array('/' . str_replace('/', '\/', $matches['expr']) . '/', $matches['file']); | |
} | |
} | |
fclose($fp); | |
} else { | |
error_log("No .htaccess file found in documnet root " + $_SERVER['DOCUMENT_ROOT']); | |
} | |
$rewriteUri = substr($_SERVER["SCRIPT_NAME"], 1, strlen($_SERVER["SCRIPT_NAME"]) - 1); | |
$match = false; | |
array_walk($rewrites, function ($rewrite) use (&$match, $rewriteUri) { | |
if (preg_match($rewrite[0], $rewriteUri)) { | |
$match = true; | |
require_once($_SERVER['DOCUMENT_ROOT'] . '/' . $rewrite[1]); | |
} | |
}); | |
return $match; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working for me