To work in a local development environment PHP provides an easy-to-use built-in web server.

The PHP built-in web server is a handy feature, but it is not an Apache nor Nginx web server. Therefore, features like Url friendly are not supported by default.

Today, it is common for web projects to have friendly URLs as a requirement, for example, Laravel applications depend on it. This is achieved through Apache (mod_rewrite) or Nginx specific configuration, but is not default on renders built-in PHP web server.

Friendly Url with PHP built in server

To simulate a Url friendly behaviour on PHP built in web server, you must to provide a PHP-based routing.

From PHP manual:

If a PHP file is given on the command line when the web server is started it is treated as a “router” script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script’s output is returned to the browser.

Following is a script, you can call it router.php, that returns false if the requested URI exists as a file, otherwise it includes index.php, that should have your code to deal with the bootstrap of application. If you are running a Laravel appplication for example, this route script would work as expected to simulate Url friendly.

<?php
$request_uri = __DIR__ . $_SERVER["REQUEST_URI"];
if (file_exists($request_uri)) {
  return false;
} 

include __DIR__ . /index.php';

We could change our route to check extensions that shouldn´t be handled with Url friendly behaviour:

<?php

$request_uri = __DIR__ . $_SERVER["REQUEST_URI"];

if (file_exists($request_uri)) {
  return false;
} 

if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $request_uri)) {
    return false;
}

include __DIR__ . '/index.php';

To start the PHP built in server pass the route.php as a parameter:

php -S localhost:8888 routing.php

Final considerations

With this simple script you probably won’t need to install Apache or Nginx in your local development environment. But, I must say that I must say that I prefer to use Apache or Nginx to have an environment closer to real server environments where my applications gonna run.

You can use Docker to let your devlopment environment easier to setup. I like to use Laradock, you can learn about it here on Laradock section.

Firendly Url with PHP built in server

To simulate a Url friendly behaviour on PHP built in web server, you must to provide a PHP-based routing.

From PHP manual:

If a PHP file is given on the command line when the web server is started it is treated as a “router” script. The script is run at the start of each HTTP request. If this script returns FALSE, then the requested resource is returned as-is. Otherwise the script’s output is returned to the browser.

Following is a script, you can call it router.php, that returns false if the requested URI exists as a file, otherwise it includes index.php, that should have your code to deal with the bootstrap of application. If you are running a Laravel appplication for example, this route script would work as expected to simulate Url friendly.

<?php
$request_uri = __DIR__ . $_SERVER["REQUEST_URI"];
if (file_exists($request_uri)) {
  return false;
} 

include __DIR__ . /index.php';

We could change our route to check extensions that shouldn´t be handled with Url friendly behaviour:

<?php

$request_uri = __DIR__ . $_SERVER["REQUEST_URI"];

if (file_exists($request_uri)) {
  return false;
} 

if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $request_uri)) {
    return false;
}

include __DIR__ . '/index.php';

To start the PHP biult in server pass the route.php as a parameter:

php -S localhost:8888 routing.php

Final considerations

With this simple script you probably won’t need to install Apache or Nginx in your local development environment. But, I must say that I prefer to use Apache or Nginx to have an environment closer to real server environments where my applications gonna run.

You can use Docker to let your devlopment environment easier to setup. I like to use Laradock, you can learn about it here on Laradock section.