19
votes

I just created my first Symfony2 project. But the "/web/app_dev.php" part in the URL annoys me. It should be possible to do this without Virtual hosts...

But when I try this through .htaccess I always get routing errors and the "web/" is always added to the url...

EDIT: The whole project is also in a subdirectory named "symfonyTest". The Url to the demo page is "http://localhost/symfonyTest/web/app_dev.php/demo/" and it should become "http://localhost/symfonyTest/demo/". Links should also have this syntax. Is this possible?

7
Would you mind showing the virtual host part for the application?Sgoettschkes

7 Answers

21
votes

Symfony2 comes with a built in debug mode, which is what you are using when you access url's with the app_dev.php addition. The debug mode caches significantly less than the production mode which can be accessed by directing your browser to the url, but leaving out the app_dev.php. If accessing this url doesn't work then it probably means that you need to clear your production cache.

To clear the cache direct you terminal (command console) to the root of you Symfony project. From there type the following command:

php app/console cache:clear --env=prod --no-debug

Per comments below in Symfony 3 this has moved:

php bin/console cache:clear --env=prod --no-debug

This should clear your productions cache and allow you to access urls without the app_dev.php.

As for the web part of the url. The easiest way to remove that is to set the web root of your project to the web folder. It's best to do this with apache using virtual hosts, but there are ways to do it with the htaccess.

This link explains how to use the htaccess file to change the webroot. http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/

Hope this helps!

11
votes
<VirtualHost *:80>
    DocumentRoot "path_to_symfonyTest/web"  
    ServerName "symfonyTest"  
    <Directory "path_to_symfonyTest/web">
        DirectoryIndex app_dev.php
        AllowOverride All
        Order allow,deny
        Allow from all
        RewriteEngine On
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app_dev.php [QSA,L]
        RedirectMatch permanent ^/app_dev\.php/(.*) /$1
    </Directory>
</VirtualHost>

That's part of my httpd.conf

10
votes

I used to have this problem too, please take a look at my configurations and try them out

inside my .htaccess:

Hi all, I have been having this problem too. And it seems that the "stock" .htaccess is the problem. I have solved this in my environment and here is my .htaccess with notes:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app_dev.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]    ##### this is the part that you     should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]
    RewriteRule .? %{ENV:BASE}/app_dev.php [L]        ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the startpage to the front controller explicitly so that the website
        # and the generated links can still be used.
        RedirectMatch 302 ^/$ /app.php/
        # RedirectTemp cannot be used instead
    </IfModule>
</IfModule>

inside my "symfony" entry in /etc/apache2/sites-available:

<VirtualHost 127.0.1.15>
    ServerAdmin webmaster@localhost
    ServerName symfony
    DocumentRoot /var/www/Symfony/web
    DirectoryIndex app.php
    <Directory /var/www/Symfony/web>
        Options Indexes FollowSymLinks MultiViews
    AllowOverride All
    Order allow,deny
    allow from all
</Directory>

ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
5
votes

If you use an apache virtual host, you can get it working the way you desire. Here is an example virtual host from my xampp:

<VirtualHost *:80>
    ServerName myurl.local

    # Basic stuff
    DocumentRoot "C:/path/to/symfony/web"
    DirectoryIndex app.php
    <Directory "C:/path/to/symfony/web">
        AllowOverride All
        Allow from All
    </Directory>
</VirtualHost>

After restarting your apache, you can access the project through http://myurl.local (don't forget to configure your hosts file under C:\Windows\system32\drivers\etc\hosts!). If you want to have the dev environment (app_dev.php) not showing up in the url, change the DirectoryIndex to app_dev.php.

3
votes
  1. //enable mod rewrite
    sudo a2enmod rewrite

  2. Change all occurrences of AllowOverride None by AllowOverride All
    File: (/etc/apache2/apache2.conf) or (/etc/apache2/sites-available/000-default.conf) - for me work with /etc/apache2/apache2.conf file.

Example:

<Directory /var/www/web/>
        # enable the .htaccess rewrites
        Options Indexes FollowSymLinks
        #AllowOverride None #commented
        AllowOverride All
        Require all granted
</Directory>
  1. //Restart apache service:
    sudo service apache2 restart
2
votes

You would need .htaccess file in parent folder of web, however, I wouldn't suggest doing so. That folder, apart from web, contains complete source, binaries, vendors and much more therefore you would need to apply many rules just to allow web-access to web folder and disallow web-access to everything else.

So, it's doable, but I would definitely go for vhost solution...

0
votes

you just need to copy the content of .htaccess file to the your-website.conf

inside the TAG Directory