Created
July 8, 2018 20:08
-
-
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
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 | |
$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