Skip to content

Instantly share code, notes, and snippets.

@ssokolow
Created July 6, 2012 03:39
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ssokolow/3057918 to your computer and use it in GitHub Desktop.
Save ssokolow/3057918 to your computer and use it in GitHub Desktop.
Basic stub for mod_rewrite-like behaviour using PHP 5.4's development server
<?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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment