mercredi 19 octobre 2011

Setup vhosts for Symfony2 development in MAMP

Let's say you are developing your Symfony2 website in MAMP, but typing "http://localhost/Symfony/web/app_dev.php/login" doesn't make you happy. You want to shorten it to somthing like : "http://mywebsite/login".

Get rid of the "app_dev.php"

To do so, you just need to modify the .htaccess file of the Symfony/web folder :
  • Check that the URL rewriting module is available
  • Turn the rewrite engine on
  • Define a rewriting rule

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On
    
    # Explicitly disable rewriting for front controllers
    RewriteRule ^app_dev.php - [L]
    RewriteRule ^app.php - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app_dev.php [QSA,L]
</IfModule>


A rewrite rule is bascally a regex that Apache applies to every incoming requested URL.
NB : the last rewrite rule needs to be changed before deploying to production.

Turn the http://localhost/Symfony/web into http://mywebsite

First, edit /Applications/MAMP/cong/apache/httpd.conf and add a new virtual host to MAMP : make sure that virtual hosts are enabled on your server : you should find that somewhere close to the bottom of the file :
NameVirtualHost *

Then you can append at the very bottom :
<VirtualHost *>
   DocumentRoot "/Applications/MAMP/htdocs/Symfony/web"
   ServerName mywebsite
</VirtualHost>

This basically says that when the mywebsite server will be called, the request will be redirected to /Applications/MAMP/htdocs/Symfony/web.
Now, edit the hosts for your computer : edit /etc/hosts and append at the end of the file :
127.0.0.1   mywebsite

This basically says that typing http://mywebsite will be redirected to the internal IP 127.0.0.1.
You are done !

Resources

Aucun commentaire:

Enregistrer un commentaire