Symfony2: Routing and mod_rewrite

Quick note on Symfony2 routing and mod_rewrite.

As I am recreating an existing project using Symfony2 rather than starting from scratch there are a few things I a doing which may not fit the usual way of working Symfony2. One issue I have run across, and which is not immediately apparent, is with using mod_rewrite to try and fit our current URL scheme for the site to one which works with Symfony2 routing.

I used mod_rewrite to rewrite some URLs behind the scenes but the changes were not being picked up in the routing. After a bit of digging through rewrite logs to check that the rewrites were taking place it became apparent that when applying routing Symfony2 uses the originally requested URL and ignores any changes made by mod_rewrite.

Symfony2 does make use of mod_rewrite to remove the app.php or app_dev.php from the URL that is needed without it. It does so though by rewriting the URL to app.php or app_dev.php with the original URL being removed rather than appended e.g.

/about/our-directors/

is rewritten to

/app.php

not

/app.php/about/our-directors/

or

/app.php?url=/about/our-directors/

This means that there is no choice but to route using the original request as PHP has no access to the intermediate rewritten URLs. This is not particularly a problem if you set out using the Symfony2 way of routing from the start but it did stop me from being able to use a bit of mod_rewrite trickery to use our current URL schema with Symfony2 routing.