Created
December 6, 2018 21:13
-
-
Save geoffroycochard/b68c53d822259cd32236dfa660c31365 to your computer and use it in GitHub Desktop.
Router php -S for WP Pretty links
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