Skip to content

Instantly share code, notes, and snippets.

@rossshannon
Created July 8, 2018 20:08
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 rossshannon/bf554b8ef7ebe85597f59a988efcdc2f to your computer and use it in GitHub Desktop.
Save rossshannon/bf554b8ef7ebe85597f59a988efcdc2f to your computer and use it in GitHub Desktop.
How to serve PHP files with .html file extensions using the built-in PHP server
<?php
$requestedAbsoluteFile = dirname(__FILE__) . $_SERVER['REQUEST_URI'];
if (!preg_match('/\.html|\.php$/', $requestedAbsoluteFile)) {
header('Content-Type: '.mime_content_type($requestedAbsoluteFile));
return false;
}
include_once $requestedAbsoluteFile;
# Usage: The built-in server included in PHP 5.4.0 doesn’t parse files with
# extension .html as PHP, so PHP features like includes or headers etc. don’t work.
# To overcome this, copy `router.php` into the root of your project directory,
# open a terminal in this directory and start the server while pointing to
# this router file:
# $ php -S localhost:8000 router.php
# Accessing localhost:8000 in your browser now should give you a fully-functioning
# server with PHP supported on these files. Files not ending in .html or .php
# are ignored by the router code.
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment