6

My website is ready to be deployed and I am trying to set it up online.

Some informations:

  • The host is OVH.
  • It doesn't allow SSH, I have to send my files with FTP. No command line either.
  • I want to be able to set up the website in a subdirectory: /www/test for now (my current website is still in /www).

The problem:

When I open the URL my-website.com/test, a Symfony exception tells me No route found for "GET /test/", which clearly means that Symfony doesn't know it is in a sub-directory.

How can I tell it?


EDIT:

I just realized it worked when I access my-website.com/test/web.

SteeveDroz
  • 6,006
  • 6
  • 33
  • 65

3 Answers3

10

Here I wrote exactly about that: https://www.refactory-project.com/install-symfony-app-in-a-subfolder-of-an-existing-site/

Upload the application part

Start by uploading the application folders at the same level of your site root:

[ftproot]
-- public_html
---- ...
---- ...
-- symfonyapp
---- app
---- bin
---- src
---- vendor
---- web
------ app.php
------ app_dev.php
------ ...
---- composer.json
---- composer.lock

Move the web part

Move the content of the "web" folder into the desired subfolder, i.e. "myapp".

[ftproot]
-- public_html
---- ...
---- ...
---- myapp
------ app.php
------ app_dev.php
------ ...
-- symfonyapp
---- app
---- bin
---- src
---- vendor
---- composer.json
---- composer.lock

Let the web know where is the application

Edit files app.php and app_dev.php and insert the new application location.

require_once __DIR__ . '/../../symfonyapp/app/bootstrap.php.cache';
require_once __DIR__ . '/../../symfonyapp/app/AppKernel.php';

Let the application know how the web folder is called

Edit file composer.json with the new web folder name

{
    ...
    "extra": {
        ...
       "symfony-web-dir": "../public_html/myapp"
    }
}
Francesco Abeni
  • 4,190
  • 1
  • 19
  • 30
  • It works perfectly! That was exactly what I was looking for. Thank you so much. On top of that, I must congratulate you for your article, it's very well written. – SteeveDroz Jul 16 '15 at 08:27
  • @Oltarus happy to hear that! Thank you. – Francesco Abeni Jul 16 '15 at 08:43
  • But will the routes work in this way? For example, a @Route("/home") should be http://example.com/subdirectory/home – the_nuts Feb 12 '17 at 15:55
  • 1
    Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Gerald Schneider Sep 13 '18 at 07:48
  • @GeraldSchneider would it be acceptable to copy / paste large chunks of the article? Because I cannot see any better / shorter way to explain it than what I wrote in the article. – Francesco Abeni Sep 13 '18 at 08:29
2

You're sort of setting yourself up for hardship if you are deploying a Symfony app to a host that does not allow SSH - for instance if you want to rebuild your cache you will have to manually nuke your app/cache/* dirs?

To answer your question directly, if your Symfony project is in /www/test/ then Symfony's web directory is /www/test/web so you need to use a url like:

http://foo.com/test/web

For completeness, try accessing the explicit url - /test/app.php and /test/app_dev.php respectively. If you receive a Symfony error page in either case you know you are on the right path at least.

Edit #1

Something to point out: your project and configuration files and may be readable with this deployment scenario - which is not ideal - so you might want to check this and take some actions to secure this directory if possible. I appreciate that this is probably a test deployment so it might not be a big deal, but it's always good to keep mindful of security :)

Edit #2

Okay YMMV with this, I am no .htaccess expert but you could deploy your symfony app to /www/symfony/ and rewrite the /test URI to show /symfony/web/ instead; e.g:

RewriteEngine On
# rewrite all `/test/*` uris to `symfony/web`
RewriteRule ^test(.*)$ symfony/web/$1 [L,QSA]
# direct access to /symfony dir is a 404
RewriteRule !^symfony/web/$1 - [R=404]

This should serve all applicable uri requests (to /test and /symfony/web itself) to Symfony, while restricting direct access to the symfony core files.

Haven't tested this, so how this will play with Symfony's own .htaccess is not something I can answer off the top of my head.

Darragh Enright
  • 13,676
  • 7
  • 41
  • 48
  • I understand the security issues, I would like the URL to show `foo.com/test`, that's my problem :-( . – SteeveDroz Jul 15 '15 at 10:20
  • Absolutely. So, your publicly accessible web root is `/www` right? Do you have any access above this path, or does your host allow you to specify another web root; e.g: `/www/public`? I'm thinking that if the answer is yes in either case, you might be able to put the Symfony project above the existing web root, and then symlink the Symfony web dir; e.g: `ln -s /www/my_symfony_project/web /www/public/test` – Darragh Enright Jul 15 '15 at 10:38
  • I have access to `/` and the public directory is `/www`. Your solution seems interresting, but again: no command line. I don't know any way to create a symlink on that server and believe me, I've tried! – SteeveDroz Jul 15 '15 at 10:55
0

How about using .htaccessfile to achieve your goal. As far as I understand your problem your symfony app works IF you access it via web folder for example foo.com/test/web

try using the following code in your .htaccess file which will sit at the root of your test directory

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
</IfModule>

please replace the domain.com with your domain name.

Shairyar
  • 3,268
  • 7
  • 46
  • 86