Monday, February 23, 2015

How to remove web_dev.php in Symfony site

When we first install Symfony, we can access the site at http://symfony.webroot/web/web_dev.php. we cannot access web folder directly. This is due to the debug mode. When we install Symfony, it comes with built in debug mode enabled. Also, we have to access site through web/app.php in production mode This might be annoying.

Symfony comes with two modes: Production mode and debug mode. Debug mode behave differently than production mode. It caches much less but shows more verbose information. We can remove this web_dev.php by clearing production mode and setting debug mode off. This can be done by running following console command at command line at the root of Symfony project

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

This command clears the production cache. It also allows to access Symfony urls without app_dev.php.

Now we can access the site at http://symfony.webroot/web/action
But if we want to  access the action through http://symfony.webroot/action, without web/ then we have to rewrite the url with .htaccess or point to web folder in apache virtual host httpd.conf

The web/ in the url can be removed through .htaccess or apache virtual hosts. While setting webroot of apache virtual host is better, .htaccess works just fine in case we don't have access to the httpd.conf file in the server.

With .htaccess

To do this with .htaccess, create .htaccess at your web root folder and write following code To only remove the web/ subfolder in url do follwoing:

  <ifmodule mod_rewrite.c>
     RewriteEngine On 
     RewriteRule ^$ web/ 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteRule ^(.*)$ web/$1
  </ifmodule>
 
It says: if there is web subfolder and if the request is not file or other directory, replace this web and access other contents inside it.

Entirely remove web/app

If we want to entirely remove web/app.php, we must explicitly name that in RewriteRule. Use following code for Symfony site and it will work.

  <ifmodule mod_rewrite.c>
     Options +FollowSymlinks
     RewriteEngine On

     # disable rewriting for front controllers
     RewriteRule ^/web/app_dev.php - [L]
     RewriteRule ^/web/app.php - [L]

     # Fix the bundles folder
     RewriteRule ^bundles/(.*)$ /web/bundles/$1  [QSA,L]

     RewriteCond %{REQUEST_FILENAME} !-f
     # Change below before deploying to production
     #RewriteRule ^(.*)$ /web/app.php [QSA,L]
     RewriteRule ^(.*)$ /web/app_dev.php [QSA,L]
  </ifmodule>

To do with virtual host, just point the /web/ directory as your document root in your httpd config file


  <VirtualHost *:80>
     DocumentRoot /www/symfony.webroot/web
     ServerName www.example.com
     # Other directives here
  </VirtualHost>


No comments:

Post a Comment