1

In my symfony I moved /MySymfonyFolder/web/app.php to root and rename it to index.php e.g. /MySymfonyFolder/index.php, I changed htaccess as below but it causes problem to find css images via cssrewrite as I wrote here.

1) I don't want to move /MySymfonyFolder/web/bundles/, /MySymfonyFolder/web/images/ and /MySymfonyFolder/web/css to /MySymfonyFolder/ too. 2) I don't want to move /app/ and /src/ in upper level outside symfony root.

Assuming I want all /index.php (symfony app.php), /app/ and /src/ in a sub-folder, how the htaccess should be to recognize both /MySymfonyFolder/index.php and /MySymfonyFolder/web/ that it can find images too?

Here is my wrong .htaccess that causes css image problem. please correct it. I searched stackoverflow and found nothing regarding this.

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    RewriteRule .? %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 302 ^/$ /index.php/
    </IfModule>
</IfModule>
Community
  • 1
  • 1
user4271704
  • 723
  • 1
  • 12
  • 37
  • you are moving the entry point and therefore base url of the application, so you are likely messing up any relative paths, for example `/bundles/mybundles/css/main.css` might now need referencing by `/web/bundles/mybundles/css/main.css`. I don't have Symfony handy at the moment to double check it, but it looks like your changing the document root, which is breaking relative links – Scriptable Jan 17 '15 at 18:56
  • Is it possible to solved everything with htaccess in /MySymfonyFolder/ root? – user4271704 Jan 17 '15 at 18:57
  • you could possibly rewrite any requests for /bundles/ to /web/bundles. There is a reason the symfony team have structured the framework in this way and if the document root is a level up then you are creating a security risk. If you can explain WHY you are doing this then we should be able to propose a better solution – Scriptable Jan 17 '15 at 18:59
  • Anyway can you suggest a better htaccess? – user4271704 Jan 17 '15 at 19:08

3 Answers3

3

You should look at the documentation!

If you need to rename or move your web directory, the only thing you need to guarantee is that the path to the app directory is still correct in your app.php and app_dev.php front controllers. If you simply renamed the directory, you're fine. But if you moved it in some way, you may need to modify the paths inside these files:

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

You also need to change the extra.symfony-web-dir option in the composer.json file:

{
    ...
    "extra": {
        ...
        "symfony-web-dir": "."
    }
}
Emii Khaos
  • 9,983
  • 3
  • 34
  • 57
  • Actually I did that and it was working but it had assets problem as I said here http://stackoverflow.com/questions/27993233/how-to-have-background-image-in-symfony-assets/27999621 what else should I do? – user4271704 Jan 17 '15 at 20:24
  • Please read comment above. I think my issue described in that question was because of read_from in assetic config as here http://symfony.com/doc/current/cookbook/configuration/override_dir_structure.html#override-the-web-directory correct? – user4271704 Jan 17 '15 at 20:31
  • This simple answer not only helped me solve my problem, but also reminded me, a beginner, that all framework structures can and should be altered to suit individual needs. Thanks! – MartinJH Apr 24 '16 at 16:02
1

Try:

RewriteEngine on
RewriteBase /
RewriteRule ^css/(.*) web/css/$1
RewriteRule ^images/(.*) web/images/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]

OR

RewriteEngine on
RewriteBase /
RewriteRule ^bundles/(.*) web/bundles/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]

All that these are doing are looking for any requests that would normally be in the web folder and pre-pending the web/ part to the request, So in theory it should work, might just need tweaking based on what files you have.

You should also look into updating the .htaccess to prevent direct access to the other application files that aren't in the web folder, Symfony makes these inaccessible for a reason. Using the root folder as your DocumentRoot is not recommanded.

Scriptable
  • 19,402
  • 5
  • 56
  • 72
  • I re-instated this answer as the point about the security risks involved in doing it this way are valid, as is the point about using the .htaccess to prevent access to the other folders. The rewrite rules weren't messed up btw. The only reason listed rewrite rules in the answer was to correct the rules used by the OP in his question. – Scriptable Jan 19 '15 at 13:04
  • @Paziツ I think that was his point, he's suggesting to keep it the 'symfony way' i.e. route the app through the web folder – Ewan Valentine Jan 19 '15 at 13:05
  • With the help of stof here https://github.com/symfony/AsseticBundle/issues/340 I was able to have app.php in root folder and keep images/, css/ and bundles/ folders within web/ folder. Now everything works fine except css background images. any idea how to solve this too? – user4271704 Jan 19 '15 at 15:27
  • you probably need to update the `url(/bundles/..)` to `/web/bundles` as thats the updated path (I think). Personally I've never tried to move the app.php folder as I know it would cause all kinds of issues such as this – Scriptable Jan 19 '15 at 15:44
  • This structure is useful only if you install project on main site folder that way other folders will be out of public and secure but if you want to install project in a folder like http://www.example.com/symfony, other folders are still public and should be secured with htaccess this is why I say this structure is useless. See this http://stackoverflow.com/questions/28032757/how-to-have-css-background-image-with-symfony-app-php-in-root-folder do you have any idea? – user4271704 Jan 20 '15 at 13:49
0

My rule of thumb is, if you're using Symfony2, it's probably going to be too big for shared hosting anyway. You'll want control over your server, especially setting document roots etc.

Consider using something such as Linode or Digital Ocean. Or re-consider whether you need a framework as full stack as Symfony2. Laravel works well on shared hosting I believe.

Ewan Valentine
  • 3,741
  • 7
  • 43
  • 68