Routing requests to index.php with Apache

March 25, 2013

Image for Routing requests to index.php with Apache from unsplash.com

Here's how to go about routing requests to index.php with Apache. I'm always having to search for this online so I thought I'd document it here. Many of the 'modern' PHP applications use index.php as a router, including WordPress, Drupal, Zend Framework, Silex etc.

Using Mod_Rewrite

If you are using Apache pre-2.4 then your only option is to use mod_rewrite to route requests to index.php:

RewriteRule ^index\.php$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L,QSA]

Using the FallbackResource Directive

From Apache 2.4 onwards you can use the new FallbackResource Directive:

FallbackResource /index.php

Documentation for this is available at http://httpd.apache.org/docs/trunk/mod/mod_dir.html#fallbackresource.