Skip to content

Instantly share code, notes, and snippets.

@geoffroycochard
Created December 6, 2018 21:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save geoffroycochard/b68c53d822259cd32236dfa660c31365 to your computer and use it in GitHub Desktop.
Save geoffroycochard/b68c53d822259cd32236dfa660c31365 to your computer and use it in GitHub Desktop.
Router php -S for WP Pretty links
<?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