Skip to content

Instantly share code, notes, and snippets.

@rotexdegba
Created February 1, 2021 17:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rotexdegba/82df49f631e7e15b50fa013cdb7d288a to your computer and use it in GitHub Desktop.
Save rotexdegba/82df49f631e7e15b50fa013cdb7d288a to your computer and use it in GitHub Desktop.
Router script for the builtin PHP webserver to handle urls ending in dot something
<?php
// This script is intended to fix the php built-in server issue with urls that
// contain a .something at the end. The built-in server tries to always treat
// such urls as static assets when sometimes they are routes meant to be
// forwarded to the index.php. See below for more info:
//
// https://bugs.php.net/bug.php?id=61286
// https://github.com/slimphp/Slim/issues/359
// https://gonzalo123.com/2012/10/15/how-to-rewrite-urls-with-php-5-4s-built-in-web-server/
// https://gist.github.com/aenglander/8e2f83c4526fccdcdead
//
$ds = DIRECTORY_SEPARATOR;
$path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$path = str_replace(["\\", '/'], [$ds, $ds], $path); // make path use OS directory separator
if (file_exists($_SERVER['DOCUMENT_ROOT'] . $path)) {
return false;
} else {
$_SERVER['SCRIPT_NAME'] = '/index.php';
require $_SERVER['DOCUMENT_ROOT'] . "{$ds}index.php";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment