Bootstrapping the Application: the .htaccess File
Friday, September 18, 2009

The crucial first stage of the bootstrapping process involves directing all requests for web pages and web services to the index.php file. On Apache, this is done by using mod_rewrite. The module needs to be enabled on the application’s web server; it also needs to be permitted for the application’s public web directory (htdocs).

If you are using shared hosting, then you are very much at the mercy of what is on offer from your ISP: if mod_rewrite is not supported, then you can’t use the full framework; on the other hand, if it is, then your task is actually simpler as you don’t have to worry about setting it up!

If you have more control over the server environment, you may be in a position to set the necessary environment yourself. I don’t propose to discuss how exactly to enable mod_rewrite here; it may not be necessary as it is already enabled on some set-ups. You should consult the relevant documentation for more details. When mod_rewrite is enabled, you need to make sure that it is permitted within you public web directory. Here is an example of how I have done this as part of my virtual host configuration on my test machine:

<VirtualHost *>
    ServerName gm-ram
    DocumentRoot /srv/www/gm-ram/htdocs
 
    <Directory /srv/www/gm-ram/htdocs>
        AllowOverride All
        Options All
    </Directory>
</VirtualHost>

The key instruction is AllowOverride All in the Directory block; this enables URL rewriting for the directory.

When you are sure that the server environment provides the necessary support, you can add the .htaccess file to the public web directory. This should contain the following instructions:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]

The result is that all the relevant requests are directed to index.php.

I shall look at what is contained in the index.php script in my next post.

Posted by James at 5:48 pm   0 comments

Leave a Reply