/ apache

Flexible domain mapping to symfony apps

Here is a quick tip for your symfony 1.3/1.4 project (this probably works with other versions as well) hosted on an Apache webserver: So you fleshed out your symfony project and you ended up with several apps, all being available under their controller name frontend.php, anotherfrontend.php etc. For your routing (and maybe SEO purposes) its a bit unsatisfying that only one app in the project can be available without adressing its front controller (using the no_script_name setting in settings.yml). However, if you do have multiple domains, you can map each domain to a symfony app in your project very flexibly by modifying .htaccess in your symfony web folder. The stock file should look like this:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

For each domain you want to redirect to another app you then insert the following lines into the rewrite rules:

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^$ exampleapp.php [L]

This will redirect the domain www.anotherdomain.com to the app controller exampleapp.php. Of course these are just examples. You need to modify this for your issues. You could, for instance use regular expressions to cover multiple domains or you could redirect the domain to a certain route instead to the front controller. You can also use this pattern for further domain routings by just inserting more of these rules. Your final result look something like this:

Options +FollowSymLinks +ExecCGI

<IfModule mod_rewrite.c>
RewriteEngine On

# uncomment the following line, if you are having trouble
# getting no_script_name to work
#RewriteBase /

RewriteCond %{HTTP_HOST} ^(www\.)?example.com$
RewriteRule ^$ exampleapp.php [L]

# we skip all files with .something
#RewriteCond %{REQUEST_URI} \..+$
#RewriteCond %{REQUEST_URI} !\.html$
#RewriteRule .* - [L]

# we check if the .html version is here (caching)
RewriteRule ^$ index.html [QSA]
RewriteRule ^([^.]+)$ $1.html [QSA]
RewriteCond %{REQUEST_FILENAME} !-f

# no, so we redirect to our front web controller
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>

This is my solution for routing multiple domains to symfony apps, I hope you like it :-)