1

I currently have two projects:

  1. /home/piotrek/Vhosts/sf.local/web/app_dev.php
  2. /home/piotrek/Vhosts/sf2.local/web/app_dev.php

Both have same repo but are set to two different branches.

I have vhost for first site:

<VirtualHost *:80>
    ServerName sf.local
    ServerAlias www.sf.local

    DocumentRoot /home/piotrek/Vhosts/sf.local/web

    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf.local/web/$1

    DirectoryIndex app_dev.php

    <Directory /home/piotrek/Vhosts/sf.local/web>
        AllowOverride All
        Require all granted
        Options -MultiViews

        <IfModule mod_rewrite.c>
            RewriteEngine On
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_dev.php [QSA,L]

        </IfModule>
    </Directory>

    ErrorLog /home/piotrek/Vhosts/logs/sf.local-error.log
    CustomLog /home/piotrek/Vhosts/logs/sf.local-access.log combined

</VirtualHost>

When I request http://sf.local/ everything works fine. But now I want to send people to /sf2.local/ when there is cookie with name THEME set. Something like this:

RewriteCond %{HTTP_COOKIE} THEME=new [NC]
RewriteRule ^(.*)$ sf2project

How can I combine mod rewrite with fastcgi proxy to server one site when there is no cookie and other when there is cookie set?

piotrekkr
  • 226
  • 2
  • 9

1 Answers1

0

Ok, so I figured it out.

Vhost should look like this:

<VirtualHost *:80>
    ServerName sf.local
    ServerAlias www.sf.local

    DocumentRoot /home/piotrek/Vhosts

    ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9071/home/piotrek/Vhosts/$1

    <Directory /home/piotrek/Vhosts>
        AllowOverride None
        Require all granted

        <IfModule mod_rewrite.c>
            Options -MultiViews
            RewriteEngine On

            # rewrite if cookie is set to "new"
            RewriteCond %{HTTP_COOKIE} THEME=new [NC]
            RewriteRule ^(.*)$ sf2.local/web/$1 [QSA,L]

            # rewrite to old version
            RewriteRule ^(.*)$ sf.local/web/$1 [QSA,L]
        </IfModule>

    </Directory>

    <Directory /home/piotrek/Vhosts/sf.local/web>

        <IfModule mod_rewrite.c>
            # local rewrite to app_dev.php if file does not exists
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_dev.php [QSA,L]
        </IfModule>

    </Directory>

    <Directory /home/piotrek/Vhosts/sf2.local/web>

        <IfModule mod_rewrite.c>
            # local rewrite to app_dev.php if file does not exists
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteRule ^(.*)$ app_dev.php [QSA,L]
        </IfModule>

    </Directory>

    ErrorLog /home/piotrek/Vhosts/logs/sf.local-error.log
    CustomLog /home/piotrek/Vhosts/logs/sf.local-access.log combined

</VirtualHost>

/home/piotrek/Vhosts/sf.local/web/app_dev.php

<?php
setcookie("THEME", "new", time() + 3600);

die('OLD THEME');

/home/piotrek/Vhosts/sf2.local/web/app_dev.php

<?php

die('NEW THEME');

Now, when I enter http://sf.local/ first time,

  1. request is rewritten to sf.local/web/app_dev.php
  2. ProxyPassMatchhandles request to old theme directory fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf.local/web/app_dev.php
  3. cookie is set
  4. OLD THEME is displayed

After refresh

  1. request is rewritten to sf2.local/web/app_dev.php
  2. ProxyPassMatch handles request to new theme directory fcgi://127.0.0.1:9071/home/piotrek/Vhosts/sf2.local/web/app_dev.php
  3. NEW THEME is displayed

Rewrites also works for other files like http://sf.local/robots.txt. Without cookie, it is rewritten to /home/piotrek/Vhosts/sf.local/web/robots.txt and with cookie to /home/piotrek/Vhosts/sf2.local/web/robots.txt

piotrekkr
  • 226
  • 2
  • 9