Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove "web/app_dev.php" from Symfony2 URLs?

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?

like image 521
phil-opp Avatar asked Mar 12 '12 14:03

phil-opp


4 Answers

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!

like image 161
Shattuck Avatar answered Nov 15 '22 12:11

Shattuck


<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

like image 25
usrmrz Avatar answered Nov 15 '22 13:11

usrmrz


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>
like image 10
Frederick G. Sandalo Avatar answered Nov 15 '22 14:11

Frederick G. Sandalo


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.

like image 5
Sgoettschkes Avatar answered Nov 15 '22 14:11

Sgoettschkes