This site uses third-party cookies, learn more or accept

Fallback resource overview

Specify a default/fallback file on a web server
Written by Maxwell Pelic,

I started web development on a Mac, and later bought a Windows computer. When I ran a testing server on the Mac, it automatically used the nearest index file if the file you requested didn’t exist (at least that’s how I thought it worked, but I didn’t have much server experience at that point and I could have been totally wrong).

When I installed Apache on my windows computer, I tried to replicate that behavior. But I had no idea what to search for due to my inexperience with servers.

Luckily I stumbled across the solution, and found it really useful when setting up a dynamic site with multiple dynamically generated pages.

Here’s an overview of how to set up a fallback resource and some best practices.

FallbackResource

You can set up a fallback resource in your httpd.conf file, or you can put it in your .htaccess file. Either way, just add the FallbackResource keyword followed by the path to your default file.


FallbackResource /path/to/file.php   

Some things to note:

Never use a relative path! When you use a relative path, it uses the directory the user specified in the url, not the directory specified, which can lead to an endless loop.

This may seem obvious, but remember if a file exists, the server won’t use the fallback resource. So if you want to serve all pages from a fallback resource, don’t include any other files in the folder.

It’s also helpful to remember that when you have a bunch of dependencies (Javascript and/or CSS) and don’t want to have to link them all in the fallback resource. You can still have a folder of them and link to them without worrying about handling them in you fallback file.

Previous Article: Run Apache on Windows without any extra software

Next Article: Free Resources for Web Devs