Serving web pages with no file extensions using MultiViews

PostJan 30th, 2006 | Comments (0)
Once in a while I'll discover something simple while designing a site that makes me feel like I've won the lottery. Such was the case last night when I discovered the "MultiViews" Apache directory option.

Now I'm not a real techie kind of guy. I know how to set up basic sites in Apache, but that's about it. So when I realized that my local server could serve pages without the need for file extensions (e.g. "http://site.com/page" instead of "http://site.com/page.php") -- but my remote server couldn't -- I did some research to try and find out why.

Google was no help, so I decided to peruse and compare my local and remote httpd.conf files. After a bit of comparing I realized that my local httpd.conf file had "MultiViews" listed under the default <Directory> settings. I looked up to see what "MultiViews" did, and it was like a bolt of lightning.

From the Apache.org docs:

MultiViews is a per-directory option, meaning it can be set with an Options directive within a <Directory>, <Location> or <Files> section in httpd.conf.

The effect of MultiViews is as follows: if the server receives a request for /some/dir/foo, if /some/dir has MultiViews enabled, and /some/dir/foo does not exist, then the server reads the directory looking for files named foo.*, and effectively fakes up a type map which names all those files, assigning them the same media types and content-encodings it would have if the client had asked for one of them by name. It then chooses the best match to the client's requirements.

Brilliant! That single word has made me a happy, happy designer. I've been using mod_rewrite for a while to manually do the same thing that "MultiViews" does automatically. No more remembering to add RewriteRules. No more Apache restarts. Life is sweet.

Here is what my new default <Directory> settings look like:

<Directory "/path/to/html">
    Options Indexes FollowSymLinks MultiViews
    AllowOverride None
    Order allow,deny
    Allow from all
</Directory>

I hope this helps other web designers out.

Comments

There have been no comments

Post a comment

Name
URL
Email
Comment