Martyn Martyn - 3 months ago 80
PHP Question

PHP built in server and .htaccess mod rewrites

Does PHP's built in server not make use of .htaccess? Makes sense I suppose as it isn't relying upon Apache(?). Anyway is it possible to tell the server to make use of these files, can it handle URL rewrites? I have some porjects in frameworks that rely upon these files.

APPLICATION_ENV=development php -S localhost:8000 -t public/

Answer

It is very usual to use mod_rewrite or similar to send all requests to the front controller. With apache is pretty straight forward to do it:

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

But, does it work with the built-in web server????

The answer is yes, but with another syntax. You need to create one router file and start our server with this router:

<?php
// www/routing.php
if (preg_match('/\.(?:png|jpg|jpeg|gif)$/', $_SERVER["REQUEST_URI"])) {
    return false;
} else {
    include __DIR__ . '/index.php';
}

And now start the server with:

php -S localhost:8888 www/routing.php