Skip to content

Instantly share code, notes, and snippets.

@mkober
Created December 2, 2017 17:49
Show Gist options
  • Save mkober/c858b3db751f1d8b266c6af99eabcde4 to your computer and use it in GitHub Desktop.
Save mkober/c858b3db751f1d8b266c6af99eabcde4 to your computer and use it in GitHub Desktop.
Wordpress Router for PHP Built-in Web Server
<?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