Using MultiViews for Pretty URL Routing

I often build small PHP web projects. Typically, I don't want to use a large MVC Framework for the project because it only consists of a few PHP files. But, in many cases, I'd still like Pretty URL's without file extensions.

http://www.example.com/post

One simple solution is to turn on MultiViews in Apache. You can do this in the httpd.conf file or in the .htaccess file. MultiViews are setup on a per directory basis. So, you'd put something like the following in your virtual host setup.

<VirtualHost *:80>
       ServerName example.com
       DocumentRoot /httpd/example/
    <Directory /httpd/example/>
        Options FollowSymLinks MultiViews
    </Directory>
</VirtualHost>

With the MultiViews option turned on, if a file for a particular URL doesn't exist, Apache will look for a close match. So, if you have a file called post.php apache will find it when you enter a URL with just /post. No rewrite rules are required.

Instead of the mod_rewrite module, MutlViews requires the mod_negotiation module. I typically find that it's already installed.

comments powered by Disqus
using_multiviews_for_pretty_url_routing.txt · Last modified: 2020/06/01 22:53 (external edit)