Tag Archives: multi-domain

Multiple domains on symfony project

This post is about developing a symfony project that provides multiple applications that are reachable on multiple top level domains. In our case, the symfony applications “UMTS Netzabdeckung” and “Flatrates Vergleich” share the same backend (models, data, etc.) but each site is a standalone application and has a dedicated top level domain.

Create two applications

The first step is to create two applications. Run

symfony generate:app site1
symfony generate:app site2

in your console and change the default actions, routing, etc. Same thing as usual.

Edit .htaccess

The next step is quite simple: Open your .htaccess file in /sf_project_dir/web and insert your second application to the .htaccess file (BEFORE the default RewriteRule)

RewriteCond %{HTTP_HOST} secondhost.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ site2.php [QSA,L]

That’s all!

That’s all to get it working. Each request to your second domain will be redirected to the according application. A drawback is, that your “frontend” application is still reachable on your second domain if you type in the script name directly. Since I’m not an htaccess expert, I just added some php code to my index.php:

if($_SERVER['SERVER_NAME'] != ‘www.flatratesvergleich.de’) {
header(‘Location: http://www.flatratesvergleich.de’ . $_SERVER['REQUEST_URI']);
die;
} Continue reading

Posted in General, Symfony | Tagged , , , , | View Comments