public
Last active

Basic stub for mod_rewrite-like behaviour using PHP 5.4's development server

  • Download Gist
index.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
// To the extent possible under law, I (Stephan Sokolow) waive all copyright and related or
// neighbouring rights to this code snippet. (Though it'd still be nice if you mention me)
 
// If we're running under `php -S` with PHP 5.4.0+
if (php_sapi_name() == 'cli-server') {
 
// Replicate the effects of basic "index.php"-hiding mod_rewrite rules
// Tested working under FatFreeFramework 2.0.6 through 2.0.12.
$_SERVER['SCRIPT_NAME'] = str_replace(__DIR__, '', __FILE__);
$_SERVER['SCRIPT_FILENAME'] = __FILE__;
 
// Replicate the FatFree/WordPress/etc. .htaccess "serve existing files" bit
$url_parts = parse_url($_SERVER["REQUEST_URI"]);
$_req = rtrim($_SERVER['DOCUMENT_ROOT'] . $url_parts['path'], '/' . DIRECTORY_SEPARATOR);
if (__FILE__ !== $_req && __DIR__ !== $_req && file_exists($_req)) {
return false; // serve the requested resource as-is.
}
}
 
// TODO: The rest of your index.php goes here

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.