1
votes

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>
3
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
Is it possible to solved everything with htaccess in /MySymfonyFolder/ root? - user4271704
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
Anyway can you suggest a better htaccess? - user4271704

3 Answers

3
votes

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": "."
    }
}
1
votes

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.

0
votes

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.