Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

As most frameworks out there, Silex depends on rewrite rules to handle pretty urls. The default .htaccess recommended for use with this framework is

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

Which is straightforward and works just fine. But if I also want to rewrite the request uri to always include a trailing slash I can't get it to work. I tried using the following rules before.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteRule . %{REQUEST_URI}/ [L]

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

To debug this, I've tried forcing a redirect using the [R] flag and I can see that it evaluates correctly but both rules together don't do the trick as the resulting request uri still remains without a trailing slash.

To sum up: should redirect to and in the end handled by index.php. should be handled by index.php without modifications.

I am not an expert in mod_rewrite but I've spent a signifficant time reading through the docs here and I have a gut feeling that this should be possible to solve this way.

Thanks in advance, Alex

share|improve this question
Other than requiring the R flag in your trailing slash rule, these work fine for me in a blank htaccess file and a default apache 2.2 install. – Jon Lin Dec 25 '12 at 0:53

It turns out apache and mod_rewrite are actually working the way they should given the setup shown in the question. The php framework was not making use of the right url.

When using php with apache, apache creates several server variables (namely the $_SERVER global).

So given the above rewrite rules, when a user navigates to apache would first rewrite to and subsequently handing everything to index.php to handle. What was not so obvious was that the $_SERVER['REQUEST_URI'] variable was not changed, rather the $_SERVER['REDIRECT_URL'] was being set with the new rewritten uri.

So the issue of not redirecting was actually the framework that never used the new global introduced by apache. To make use of this global variable, extend the existing Silex/Application.php and overwrite its run() method like so:

class Application extends Silex\Application
    public function run(Request $request = null)
        if (null === $request) {
            if (isset($_SERVER['REDIRECT_URL'])) {
                $_SERVER['REQUEST_URI'] = $_SERVER['REDIRECT_URL'];
            $request = Request::createFromGlobals();
        $response = $this->handle($request);
        $this->terminate($request, $response);


Hope this helps!


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.