Usually when you want to serve static content from within a dynamic application, you have to designate a special folder, such as /static/ (or have your code serve static files). The web server then understands that a request hitting /static/ should be served from the file system and not by the application.

Below is an Apache configurion for layering a FastCGI application on top of a static directory using mod_alias and mod_rewrite. With this method, you can put static files anywhere you want, if a file exists in your DocumentRoot, then Apache will serve the file instead of letting your code handle the request.

    DocumentRoot /var/www/htdocs

    # Set up virtual file as http://example.net/.fcgi
    FastCgiExternalServer /var/www/htdocs/.fcgi -host 127.0.0.1:8000

    # First, alias the static content to http://example.net/htdocs
    Alias /htdocs /var/www/htdocs/

    # ... configure the virtual fastcgi file, as usual (if no static file exists,
    # the request will get passed to your application)
    Alias / /var/www/htdocs/.fcgi/

    # Make sure the rewriting phase skips http://example.net/htdocs
    # and http://example.net/.fcgi
    RewriteCond $1 !^(htdocs|.fcgi)/?$

    # If a file OR directory matching the request exists...
    RewriteCond "/var/www/htdocs/$1" -f [OR]
    RewriteCond "/var/www/htdocs/$1/index.html" -f

    # ...then rewrite the request to use our fake static
    # content alias (which is actually the DocumentRoot)
    RewriteRule ^/(.*)$ /htdocs/$1

    # If the request is for a directory, then make sure to serve up index.html
    RewriteCond "/var/www/htdocs/$1" -d
    RewriteRule ^/htdocs/(.*)/?$ /htdocs/$1/index.html

    # Finally, signal that rewriting is done and pass-through
    # the request to the alias handler
    RewriteRule ^/htdocs/ - [L,PT]

I fleshed this out with Catalyst in mind, but it should work for any mod_fastcgi application.


π