Created
December 2, 2017 17:49
-
-
Save mkober/c858b3db751f1d8b266c6af99eabcde4 to your computer and use it in GitHub Desktop.
Wordpress Router for PHP 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 | |
// Extracted from the `wp-cli` project. https://wp-cli.org/ | |
$root = $_SERVER['DOCUMENT_ROOT']; | |
$path = '/'. ltrim( parse_url( urldecode( $_SERVER['REQUEST_URI'] ) )['path'], '/' ); | |
if ( file_exists( $root.$path ) ) { | |
// Enforces trailing slash, keeping links tidy in the admin | |
if ( is_dir( $root.$path ) && substr( $path, -1 ) !== '/' ) { | |
header( "Location: $path/" ); | |
exit; | |
} | |
// Runs PHP file if it exists | |
if ( strpos( $path, '.php' ) !== false ) { | |
chdir( dirname( $root.$path ) ); | |
require_once $root.$path; | |
} else { | |
return false; | |
} | |
} else { | |
// Otherwise, run `index.php` | |
chdir( $root ); | |
require_once 'index.php'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment