Skip to content

Instantly share code, notes, and snippets.

@iiidefix

iiidefix/.htaccess

Last active Sep 27, 2018
Embed
What would you like to do?
A simple classless PHP regex router function
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ ./router.php/$1 [QSA]
<?php
function route($regex, $callback) {
$regex = '/^' . str_replace('/', '\/', $regex) . '$/';
if (preg_match($regex, $_SERVER['REQUEST_URI'], $params)) {
array_shift($params);
return call_user_func_array($callback, array_values($params));
}
}
route('blog/(\w+)/(\d+)', function($category, $id){
print $category . ':' . $id;
die(); //stop processing more routes
});
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment