3

What is the best way to deploy symfony2 application in a subdirectory or an alias ?

Lets say that my application will run under: http://domain/symfonytest

symfonytest is an alias to some directory in my filesystem.

Is there some CLI operation that I can use ?

When I try to configure it manually I see a problem with routing.

Requets to http://domain/symfonytest/demo are seen by router as /symfonytest/demo

Is there a way to tell router to ignore /symfonytest prefix for the whole application ?

meze
  • 14,975
  • 4
  • 47
  • 52
Sebastian Dusza
  • 2,470
  • 2
  • 30
  • 54

3 Answers3

11

If you are using Symfony < 2.3 you could follow this approach:

Just add RewriteBase in your .htaccess:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /symfonytest
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>
Kees Schepers
  • 2,248
  • 1
  • 20
  • 31
meze
  • 14,975
  • 4
  • 47
  • 52
  • Thanks, i forgot about RewriteBase :) – Sebastian Dusza Sep 14 '12 at 17:20
  • 4
    @lucian303 no need to downvote outdated info - many people are still on 2.0 or 2.1 and this can be helpful. It's not my fault that the question doesn't have the proper tag, and you can just edit the tags. For 2.3 you don't need to do anything, it seems to be supported out of the box. – meze Jun 13 '13 at 06:26
  • @meze Sorry about that. You're right. Unfortunately, it isn't supported out of the box for 2.3 either (except on paper). They tried hard but failed miserably. – lucian303 Jun 13 '13 at 23:29
  • @meze you could update the answer specifying which up to which version this is necessary. – David Riccitelli Nov 03 '13 at 08:58
  • according to https://github.com/symfony/symfony-standard/commits/master/web/.htaccess the htaccess in symfony-standard introduced base detection in Mar/Apr 2013. – David Riccitelli Nov 03 '13 at 09:12
  • How about Symfony 2.5 is this valid for this version? – Shairyar Sep 17 '14 at 09:54
  • @Baig you don't need to do anything for 2.5 – meze Sep 17 '14 at 12:32
  • I think I do the `web` is supposed to be the root folder but the default root folder is public_html unless some sort of redirect is used to point to `web` how users can access the font end? – Shairyar Sep 17 '14 at 12:45
  • You can either: 1) configure apache DocumentRoot to `web` 2) create a symlink instead of `public_html` directory 3) rename your `web` to `public_html` – meze Sep 17 '14 at 13:46
  • Tried it with Symfony 2.5, works as expected. Thanks ! – Baptiste Gaillard Oct 23 '14 at 18:03
6

If your project sits in a different directory what i would do is set all traffic to go into one file via rewrite rule:

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

And then I would set route prefix to my controller by annotation:

# Index (main page)
_nameRouteIndex:
    resource:  "@MyBundle/Controller/IndexController.php"
    type: annotation
    prefix: /symfonytest

After that you put in your controller something like this (path would be domain/symfonytest):

/**
 * Home page
 *
 * @return Response
 *
 * @Route("/", name="_index")
 */
 public function indexAction()
 {
 }

Path: domain/symfonytest/demo

/**
 * Demo page
 *
 * @return Response
 *
 * @Route("/demo", name="_demo")
 */
 public function demoAction()
 {
 }

Hoep that helps

TroodoN-Mike
  • 15,687
  • 15
  • 55
  • 78
0

For symfony3 it works with an alias in the apache config:

Alias /myapp /home/httpd/myapp/web
segli
  • 230
  • 2
  • 6