Skip to content

Instantly share code, notes, and snippets.

@rosswintle
Last active August 2, 2024 16:42
Show Gist options
  • Save rosswintle/f516820849ad0a883151e398645df423 to your computer and use it in GitHub Desktop.
Save rosswintle/f516820849ad0a883151e398645df423 to your computer and use it in GitHub Desktop.
PHP web server router for WordPress
<?php
/**
* A PHP appliction router for WordPress. Can be used with command line PHP to run WordPress:
*
* php -S localhost:8000 ./router.php
*
* This is a minimal version of the router from WP-CLI's "server" command.
*/
define('ABSPATH', __DIR__ . '/');
if (preg_match('/\.(?:png|jpg|jpeg|gif|css|js|webp|aiff)$/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
return false; // serve the requested resource as-is.
} elseif (preg_match('/\.php$/', parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH))) {
require ABSPATH . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
} elseif (file_exists(ABSPATH . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . 'index.php')) {
require(ABSPATH . parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH) . 'index.php');
} else {
require 'index.php';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment