Serving Drupal 7 with built-in PHP 5.4 server

drupal, drupal-7, php, webserver

Solution

The task is basically to encode Drupal's .htaccess in PHP for your `router.php` file.

Here's a start:

<?php

if (preg_match("/\.(engine|inc|info|install|make|module|profile|test|po|sh|.*sql|theme|tpl(\.php)?|xtmpl)/", $_SERVER["REQUEST_URI"])) {
  print "Error\n"; // File type is not allowed
} else
if (preg_match("/(^|\/)\./", $_SERVER["REQUEST_URI"])) {
  return false; // Serve the request as-is
} else
if (file_exists($_SERVER["DOCUMENT_ROOT"] . $_SERVER["SCRIPT_NAME"])) {
  return false;
} else {
  // Feed everything else to Drupal via the "q" GET variable.
  $_GET["q"]=$_SERVER["REQUEST_URI"];
  include("index.php");
}

This should be considered alpha quality. It represents a 3 minute walk through Drupal 7.14's .htaccess file, skipping anything that needed more than 10 seconds of thought. :)

It does, however, allow me to launch Drupal's install script, with stylesheets, JS and images loaded as expected, and hit Drupal's pages using Clean URLs. Note that to install Drupal in this environment, I needed a patch that may not become part of Drupal 7.

Problem

I am looking to develop a Drupal 7 site using PHP's built-in server. I have successfully run Drupal without clean urls (e.g. index.php?q=/about/) but clean urls (e.g. /about/) normally rely on mod_rewrite or its equivalent. In the docs I see you can run the PHP server with a router file like so: ``` php -S localhost:8000 routing.php ``` What should I put in the routing.php to simulate mod_rewrite?

Original source