Recently, a junior developer asked me about how to configure Apache web server to serve URLs without the .php extension, as client asking for SEO friendly URLs. For example, instead of https://example.com/hello.php
, he was looking to have https://example.com/hello
to access the same page.
I told him to do some research. He came back with a few options which could be narrowed down between using mod_rewrite rules, and Multiview option. Both would be able to get rid of the use of .php at the end of URL, to get so-called SEO friendly URLs, which I would say it is more with a better user experience than any real SEO value. To pick between the two, let’s look at what each one is actually doing and what could be archieved by the method.
Multiview Option
MultiView option is coming from mod_negotiation, an Apache extension module to handle Content Negotiation as described in the HTTP/1.1 specification. A HTTP resource can be available in several representations, e.g. languages, media types, encoding, etc. Content negotiation help to get the correct resource usually based on the current user setting in the browser. For example, you have set your preferred language as French, if a HTTP resource is available in both English and French, you will get the French version automatically.
In order for Apache to provide variants of a resource, content creator can use a type-map file which tells the variants explicitly. Another method is to enable the MultiView option on a particular resource.
Multiview is a per-directory opion that can be set in httpd.conf file, or in .htaccess file. It needs to be explicitly specified. Options All would not include this option automatically. By enabling MultiView option on directory, you basically fake a type-map file that names, say, /hello, and all variants of /hello.* with the same media type and content-encoding. If /hello does not exist, server can look for any /hello.* and serve it instead.
By enabling MultiView option on a resource, you can basically use the SEO friendly name for any file extension to serve. So, https://example.com/hello
would be able to access https://example.com/hello.php
, so as https://example.com/hello.html
, or any type of file extension, if it exists. Apparently, this is quite handy to use. You just need to add this line to a Directory block you want:Options Multiviews
This will enable so-called SEO friendly URLs without much hassle. In fact, in a lot of default Apache PHP configuration.
mod_negotiation and MultiView option are powerful when used correctly. However, the side effect of MultiView is actually not quite SEO friendly. It opens up the door of accidental content duplication from search engine point of view. Let’s use the same previous example https://example.com/hello
. By enabling the MultiView option on this directory, the same resource, i.e. the same page content, can be accessed through these URLs.
The original file:https://exmaple.com/hello.php
The intended SEO friendly URL:https://example.com/hello
And endless number of fake unintentional combination of fake URLs:
https://example.com/hello/fakedir1
https://example.com/hello/fakedir1/fakedir2…
Most likely those unintentional URLs would not be linked from anywhere. It might be safe for small site. However, you could never be able to control how other people would link to your site. If you have a big site and SEO is important, and those unintentional URLs got picked up by search engine, unavoidably and unintentional the SEO effort would get diluted.
mod_rewrite
mod_rewrite is an Apache extension module to rewrite requested URLs on the fly based on specified rules. It could easily be one of the most powerful and extensive used Apache module. Some examples of its usages are re-directing from www to non-www site, from non-SSL to SSL, from unused URLs to userful URLs, etc. Of course, we can use it to re-direct from .php URLs to the non-extension corresponding URLs.
To properly use mod_rewrite to to re-direct from .php URLs to the SEO friendly corresponding URLs, we need to remember we do not just want to allow visiting .php without the extension, we also want to do a 301 re-direction for all the .php to the corresponding non-extension URLs. Here is an example mod_rewrite rules:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
The first part of the rewrite rules is trying to match all the .php request and return a 301 re-direction to the corresponding non-extension URL. This is important, especially, if you are converting from existing .php URLs, which have already been picked up by search engine before. The 301 re-direction would tell search engine to convert the existing SEO effort to the new URLs.
The second part of the rewrite rules will internal re-write the non-extension URL request and make a call to the actual corresponding .php file. These 2 parts together will ensure the .php URLs getting the proper 301 re-direction while the non-extension URLs are proper mapped back to the .php files.
TL;DR
The MultiView option is very handy and easy to set up. However, it has hidden consequent of opening up all sort of duplicated content. For a big site will lots of URL links, this could quite a disaster if it is not handled properly.
On the other hand, the mod_rewrite rules and its syntax are far from user friendly. It is harder to understand and more complicated to set up. However, it ensures the proper 301 redirection and do only the thing you want to do. No hidden clause.
Remember the original purpose of this argument is to get some SEO friendly URLs. Improper use of MultiView clearly creates some SEO “unfriendly” side effect. If you are not sure what MultiView can do, I would suggest to stay away from it, and stick with mod_rewrite.