I have this rewrite rule to direct request to my symfony 2 application:

RewriteCond %{REQUEST_URI}           ^/foo/.*$
RewriteRule ^/foo(/.*)$              /srv/webapps/symfony2/web/app.php [QSA,L]

If I access http://test-server.com/foo/myController I get a symfony exception telling me that it doesn't know how to route foo/myController.

I don't want to add foo to the route in symfony since the application has to work with other URL prefixes as well. How can I make sure that the foo part is not visible by symfony?

  • I cannot find an answer to my question at the mentioned link. Also the preconditions mentioned in my answer ar not mentioned at all. – BetaRide Dec 5 '14 at 11:32
  • 1
    It is a canonical article. Those are articles where the community has said all it is going to say on a class of subject, because although everyone's particular problems in that class are somewhat different, to the extent that they are interesting, they aren't different, and to the extent that they're different, they aren't interesting (to anyone save the questioner). So we write one answer that's designed to be the last word on the subject, and say no more. – MadHatter Dec 5 '14 at 11:34
  • 1
    I fail to see how that can possibly be interpreted as any more constructive than reading the documentation for the software in question (ie, that seems to obviate the entire point of this site)? – BE77Y Dec 5 '14 at 12:23

After all I got it working. My solution looks like this:

Alias /foo /srv/webapps/symfony2/web
<Directory /srv/webapps/symfony2/web >
      AllowOverride None
      Order allow,deny
      Allow from all
      Options +FollowSymLinks

      RewriteEngine On
      RewriteOptions Inherit
      RewriteBase /foo

      # prevent looping from internal redirects
      RewriteCond %{ENV:REDIRECT_STATUS} 200
      RewriteRule ^ - [L]

      ## redirect everything to app.php unless the file realy exists in /web
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule ^ app.php [last]

I don't know why this happens, but it seams to be important that alias and directory point directly to the symfony web folder. If they point to /srv/webapps/symfony2 and the final rewrite points to web/app.php you get the alias root prepended to the URI.

|improve this answer|||||
  • Indeed you do need the Alias tag as per the documentation - apologies, I should have included this rather than assuming it existed in your config outside what you posted - but other than that, you have basically gone with my solution below? – BE77Y Dec 5 '14 at 11:40
  • Yes. My main mistake was that the alias/directory was pointing to the symfony folder and not directly to the web folder. I still do not understand why this is a problem. But after all it's working. – BetaRide Dec 5 '14 at 12:18
  • Glad you got it working at least! I've edited my post to include the Alias directive in case anyone else happens upon this post with the same issue. – BE77Y Dec 5 '14 at 12:21

As it appears that you are attempting to redirect requests made to a specific context path (foo) to a single file, perhaps you would find the following (modified from this apache documentation) useful: Alias /foo /srv/webapps/symfony2/web RewriteBase /foo

  RewriteCond /srv/webapps/symfony2/web/%{REQUEST_FILENAME} !-f
  RewriteCond /srv/webapps/symfony2/web/%{REQUEST_FILENAME} !-d
  RewriteRule ^ app.php [PT]

Edit: Alternatively, you could amend your original solution to include the PT flag to pass the rewritten URI through to the application for processing, eg:

RewriteCond %{REQUEST_URI}           ^/foo/.*$
RewriteRule ^/foo(/.*)$              /srv/webapps/symfony2/web/app.php [QSA,PT]

(note that L is implied by PT as noted in the documentation

|improve this answer|||||
  • Thanks for the answer. Unfortunately this did not change anything. I'm still getting foo/myController instead of myController. – BetaRide Dec 4 '14 at 15:55
  • Have you tried your original solution with the [PT] flag included to passthrough the processed URI to the application? – BE77Y Dec 4 '14 at 16:25
  • Yes. I tried all this variations. I keep getting foo/myController. To me this looks like that mod_rewrite makes sure the right php script is executed but never changes the original URI which is passed to the php script. – BetaRide Dec 5 '14 at 10:02

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