Search code examples
.htaccesssymfonymod-rewritelegacy

Wrapping legacy Application in Framework


I have a legacy-Application with bad code and very untested. It's a large one and so we don't have the manpower to develop a new version in one step.

So I'd like to wrap it into a Symfony application and write the new parts in Symfony. The old classes will then be refactored step by step.

I tried to include the legacys frontcontroller in the application if symfony fires a 404. That works well. But if i use the app now, i have errors with it's old routing system and symfonys .htaccess.

The symfony .htaccess looks like this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]

And the legacy .htaccess looks like this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !\.(css|jpg|png|gif|js|xml)$ [NC]
RewriteRule ^(.*)$ index.php?_shop_=$1 [L,QSA]

The _shop_ Parameter is used for routing etc. and the Router of the old application depends on it. Any idea how i can geh this together?


Solution

  • If you want to fix this in .htaccess, you have to define the valid routes.

    Let's assume your old applications has the following possible routes and redirects:

    /listing => index.php?shop=listing
    /detail => index.php?shop=detail
    /cart => index.php?shop=cart
    

    Then you'll want your .htaccess to look like this

    RewriteCond %{REQUEST_FILENAME} (listing|detail|cart)
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !\.(css|jpg|png|gif|js|xml)$ [NC]
    RewriteRule ^(.*)$ index.php?_shop_=$1 [L,QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
    

    If this line is too simple for your needs, you can leverage the fact that in rewrite rules the OR flag has an higher precedence, and write something like:

    RewriteCond %{REQUEST_FILENAME} ^listing/something$ [OR]
    RewriteCond %{REQUEST_FILENAME} ^detail/[complexRegex] [OR]
    RewriteCond %{REQUEST_FILENAME} ^cart/...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !\.(css|jpg|png|gif|js|xml)$ [NC]
    RewriteRule ^(.*)$ index.php?_shop_=$1 [L,QSA]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
    

    If you want something even more advanced, you'll have to tackle it at the application level (i.e. within index.php or app.php) and not in .htaccess