Change DocumentRoot For AppFog PHP Apps

🦖 This post was published in 2013 and is most likely outdated.

AppFog doesn’t allow to edit the virtual host configuration. We need a workaround.

The problem

If you are working with a framework like Symfony2, the front controller index.php is not in your app root directory:

/path/to/my-website   <-- root directory
+- src/
+- vendor/
+- web/               <-- document directory
   +- index.php
   +- .htaccess

And we don’t want to have to include the web/ directory in all the adresses like:

http://www.my-website.com/web/index.php

We want adresses like:

http://www.my-website.com/index.php

Usually, we achieve that goal by setting the document root to be web/ in the Apache virtual host configuration:

ServerName www.my-website.com
DocumentRoot /path/to/my-website/web

But in AppFog, we can’t edit the virtual host configuration.

The fix

The workaround proposed by AppFog uses URL rewriting. Let’s add a .htaccess in the root directory /path/to/my-website to set the document root. Maybe you already have one in web/, it doesn’t matter, it’s a different one.

RewriteEngine on
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]

As it’s not likely that your need fits exactly the previous example, let me explain it.

  • The RewriteEngine on activates URL rewriting for this directory and all the sub directory.
  • The RewriteCond directive specifiers under which conditions we are going to rewrite the URL: if it doesn’t already start by web/.
  • Finally RewriteRule specifies how the URL gets rewritten if the condition if true: prepend web/.

Side note

AppFog proposes this configuration file adding two RewriteCond directives to check the domain names:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^my-website.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.my-website.com$
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]

But there is no specific reason to check the domain name. The simpler .htaccess is just rewriting for whatever domain name. It’s also convenient if you have different environment (dev, prod, test, …) that have the same code but different domain names, like dev.my-website.com.

Comments