Roger Stringer   About ▾

I'm Roger Stringer: a father, writer, developer, consultant, chef, speaker. Founder of TheInterviewr.

~•~
~•~
~•~

How to rewrite urls with PHP 5.4’s built-in web server

 

With the release of PHP 5.4, we were given a handy built-in web server. This server is obviously not suitable to use in production environments, but it is great if we want to check on one project quickly:

  • git clone from github
  • composer install to install dependencies
  • run the built-in web server and test the application.
php -S localhost:8888

But it’s also common to use mod_rewrite or similar to send all requests to a master controller. Usually, we’d have a setup like this:

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

But PHP’s built-in web server doesn’t handle mod_rewrite, and when it came to working on projects, I needed a way to make this work for development. So I came up with this solution of creating one router file and then start the server with it:

And now we start the server with:

php -S localhost:8888 routing.php

All processes are directed through routing.php which then redirects all non-CSS, JS or image requests to index.php and from there the rest is history.

Handy right?

Filed Under: Code

~•~
~•~
~•~
~•~

Growing up Okanagan

 

ksurf

I live in the Okanagan valley in British Columbia, more specifically in Penticton.

This area is interesting as we are surrounded by mountains on two sides and lakes on the other sides. The end result is most kids grow up skiing in the winter, and hitting the lake in the summer. We have Lake Okanagan on one side, and Skaha Lake on the other side, both with sandy beaches that are groomed daily.

The picture above is my daughter, as she suits up to spend a day out on the lake on her body board, her new favourite summer hobby, along with a dozen or so other kids all doing the same thing.

Filed Under: Images

~•~
~•~
~•~