Symfony4 routing inside a subfolder

前端 未结 2 688
闹比i
闹比i 2020-12-22 10:00

I have a symfony app in my /var/www/html/app/ and I\'d like to access it via host/app/ but I can\'t make it work.

I have a conf fil         


        
相关标签:
2条回答
  • 2020-12-22 10:02

    The problem is the that your web server's document root (the directory that apache will make public via the url) and your project's document root (the public directory) are not the same. Instead your server points to the project directory. That is why you have to manually go into the project's document root by adding the public/ to the url.

    You should avoid making the project directory accessible as this could expose your configuration and thus make your website vulnerable, e.g. by giving people access to your database through the credentials inside the config.

    You might be able to achieve your goal by storing the project somewhere else and then only symlink your project's public dir to /var/www/html/app. This will probably require some changes to your apache config, e.g. adding Options FollowSymLinks.

    Another, more elaborate, approach could be using mod_proxy. The configuration will roughly look like this:

    # Your public server reachable from the outside
    <VirtualHost *:80>
        ProxyPass /app http://127.0.0.1:8080/
    </VirtualHost>
    
    # Your internal server where the requests are proxied to
    <VirtualHost 127.0.0.1:8080>
        <Directory /var/www/html/app/public>
            AllowOverride All
            Order Allow,Deny
    
            Allow from All
        </Directory>
        ...
    </VirtualHost>
    

    As you can see the internal web server points to your project's document root (public dir). You might have to do additional both in the apache setup and app, e.g. make sure trusted proxies are set correctly or adding ProxyPreserveHost On to the apache setup, but roughly this should point you in the right direction.

    edit: I missed another obvious solution, using mod_alias to point your symfony app to /app:

    Alias /app /var/www/html/app/public
    
    0 讨论(0)
  • 2020-12-22 10:12

    I managed to resolve the problem !!!

    I'm not sure the config is "proper" though but it seems to work.

    Here is my working app.conf file (in /etc/apache2/sties-enabled/app.conf)

    Alias /app "/var/www/html/app/public"
    
    <Directory /var/www/html/app>
    
        AllowOverride None
        Order Allow,Deny
    
        Allow from All
    
        <IfModule mod_rewrite.c>
    
            Options -MultiViews
            RewriteEngine On
    
            RewriteRule (/public) - [L]
    
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
    
            RewriteRule ^(.*)$ index.php/$1 [L]
    
        </IfModule>
    </Directory>
    

    The first RewriteRule is to prevent from internal Rewrite loop when I rewrite url like example.com/app/page-1 to example.com/app/public/index.php/page-1. As page-1 is not a directory or a file it will loop infinitely. So I assume as soon as I have a /public part, the rewrite worked and we stop the rewrite by doing nothing and end the rewrite with the [L] flag.

    I'm not suire why I had to remove the public/ part in the "final" RewriteRule (RewriteRule ^(.*)$ index.php/$1 [L]) though.

    If someone have ideas to improve this config, feel free to leave comment.

    0 讨论(0)
提交回复
热议问题