Daily Archives: April 8, 2017

A better .htaccess for Silex/Symfony Applications

I am playing with Silex / Symfony for writing Tsugi applications and had to come up with a better .htaccess file because my old one did not route the “/” into the fallback resource correctly.

The problem is that there is a directory for “/” that stops the final rewrite url from happening because of the (red) RewriteCond.

So we add two very explicit rewrite statements (green) to handle the “standalone slash” and the “standalone slash followed by a query string”.

    <IfModule mod_rewrite.c>
        RewriteEngine on
        RewriteRule ^ - [E=protossl]
        RewriteCond %{HTTPS} on
        RewriteRule ^ - [E=protossl:s]

        # Root folder all alone
        RewriteRule "^/$" silex.php [L]
        # Root folder with GET parameters
        RewriteRule "^/?.*$" silex.php [L]

        RewriteRule "(^|/)\." - [F]
        RewriteCond %{REQUEST_FILENAME} !-f

        RewriteCond %{REQUEST_FILENAME} !-d

        RewriteCond %{REQUEST_URI} !=/favicon.ico
        RewriteRule ^ silex.php [L]
    <IfModule !mod_rewrite.c>
        FallbackResource silex.php

I have a feeling it won’t work well for the FallbackResource – as it will find the “.” folder and not “fall back”. So this kind of means I need mod_rewrite.

Alternatively just rename ‘silex.php’ to ‘index.php’ if I don’t want to have a separate index.php.