In a previous article we shared instructions on how to use .htaccess to remove file extensions in the URI (Remove .php / .html extension from URL slug). While effective, this can introduce issues in .htaccess with redirects, and other options and rules you might deploy on your site.
A cleaner way to handle these file extensions is to use the MultiView option in Apache.
The MultiView option is actually a content negotiation rule in Apache, which means for it to work you will need the mod_negotiation module enabled. The MultiView options specifically helps hide extensions.
For example, let's say you have yourdomain.com/coolpage.php but you don't want the URI to show the ".php" extension. MultiView would help you clean it so that it looks like this: yourdomain.com/coolpage (notice the .php extension is gone).
Enabling and disabling can be as simple as adding the +MultiViews (to enable) or -MultiViews (to disable) directive in your apache2.conf, httpd.conf, virtualhosts, or .htaccess files.
This option is set to OFF by default, even when you use Options ALL so be prepared to explicitly declare the state you want it in.
We've compiled a few of the issues we've run into, or seen some of our clients struggle with when it comes to using MultiView options.
Because MultiView is a content negotiation rule, you have to verify that the negotiation module is enabled.
The quickest way is to create a info.php file in a directory and add the following code:
<?php phpinfo() ?>
Open the file in the browser and look for the loaded modules section:
If it's not loaded, an easy way to enable it is to use the IfModule directive. It would look something like this in your file:
<IfModule mod_negotiation.c> Options +MultiViews </IfModule>
There might be an instance where you have configured everything correctly, but it's still not working. First place we recommend going, is your error logs. If you see something like this:
[Wed Jun 22 16:17:41.458297 2022] [negotiation:error] [pid 1143] [client IP] AH00687: Negotiation: discovered file(s) matching request: /var/www/yourdomain.com/path/file (None could be negotiated).
Then it's telling you it's unable to negotiate the conversion. In this instance, you might need to specify the type of files to negotiate. You can add this to your config file and it should do the trick:
AddType application/x-httpd-php .php .phtml .html .htm
When it is all said and done, here is an example of what it might look like in your apache2.conf file:
<Directory /var/www/yourdomain.com> Options Indexes FollowSymLinks MultiViews AllowOverride all Require all granted AddType application/x-httpd-php .php .phtml .html .htm </Directory>
All is not always great with MultiView, there may be conflicts to aware of.
For example, if you have a file called example.php in root folder and you set up a rule in your htaccess for a virtual folder called example/ then you'll have a problem with your rule because the server will choose example.php automatically (if MultiViews is enabled, which is the case most of the time).
To get around this you will want to disable MultiView like this:
And if it's a shared host, you might have to use the <IfModule mod_negotiation.c> directive as explained above.