2

I'm in the process of switching web hosts. My old host was of the beginner-friendly 'kid gloves' variety. I had less control, but it took care of a lot of things behind the scenes.

My new web host will allow me to do more with my site, but only the bare minimum is set up out of the gate. The web server won't do anything extra unless I specifically tell it to. I now need to learn a bunch of new "obvious" knowledge about managing a server.

When testing my site on the new host one thing I quickly realized is the server won't even open links to the site that are missing a file extension (e.g., www.example.com/article99)

Newbie me is trying to figure out how to set up the server to be able to go "Okay, this link to /photo37 has no extension, but I see there's an /photo37.png here in the directory. That must be what the link is referring to, so I'll open that".

From what I've been able to research so far the way to do this is to enable MultiViews. When I add Options +multiviews to my .htaccess file it seems to do the trick. But I want to double check that this is the proper way to do it, and that I'm not missing something obvious. Any help or links to resources are much-appreciated.

4
  • Arguably, the much preferred way (though it entails more work) is to use mod_rewrite - - stackoverflow.com/q/5534132/3395469 Sep 13 '17 at 3:34
  • If I used mod_rewrite, I'd have to set up rules to handle every type of extensionless file right? I.e., one for .html, one for .jpg, one for .png, etc.
    – Chris M
    Sep 14 '17 at 1:20
  • You do have to account for the extensions you wish to hide, but often these are limited enough in scope not to matter too greatly. This question has a basic outline for hiding multiple extensions with mod_rewrite and avoiding issues with looping - - stackoverflow.com/q/9157232/3395469 Sep 14 '17 at 1:44
  • "one for .html, one for .jpg, one for .png" - As a general rule you'd only want to use "extensionless URLs" for your main document URL (which is what the above reference link relates to... .htm, .html, .php, etc.). Your static resources (images, CSS, JS, etc.) should all have an extension. If you went extensionless for everything then you are increasing your dependency on MultiViews, moving away to another method/system would be more complex and prone to error.
    – MrWhite
    Sep 14 '17 at 15:59
4

tl;dr Yes, you can enable MultiViews to serve extensionless URLs. ie. Where the file extension is omitted from a URL that would otherwise map to an existing file. However, be aware of potential conflicts with mod_rewrite.

the server won't even open links to the site that are missing a file extension

You make it sound that this should be normal (or expected) behaviour? It is not. On most (database driven / CMS) sites these days, this behaviour would be undesirable. MultiViews is not enabled by default on Apache. It must be explicitly enabled.

As you have found, some shared hosts do appear to enable MultiViews automatically for its customers. So, extensionless URLs "magically" work out of the box. However, if you then want to do something more complex and rewrite/redirect the URL using mod_rewrite then you can end up with some unexpected conflicts which can be confusing to debug. This would seem to be a reasonably common problem - StackOverflow in particular is littered with questions of this nature, where the solution is to simply disable MultiViews (because it's been automatically enabled by the host).

The conflict with mod_rewrite comes about because mod_negotitation (which drives MultiViews) runs before mod_rewrite. For example, when you request /article99 with MultiViews enabled, mod_negotiation issues an internal subrequest for /article99.html (for example). You can't intercept this with mod_rewrite, you will need to match against the resulting subrequest, ie. /article99.html, not /article99 (which you might have expected since this is the URL the user actually requested and the only URL you actually see).

I want to double check that this is the proper way to do it

Well, it is one "way to do it". Yes, it is "proper". However, it is not the only way to do it. And is not always the correct way to do it. It depends on exactly what you are trying to do and what else you are already doing. But if it works for you; use it.

MultiViews does more than simply append a file extension. The resource that is returned depends on the request (this is the "negotiation" part of mod_negotiation). And which is returned when you have article99.php, article99.html and article99.jpg? And if the only file you have is article99.abc then you'll likely get a 404 (unless you have a handler defined for .abc files).

Also consider that, if you simply use MultiViews, the file article99.html (for example) is still accessible as well. So, you have the URLs /article99 and /article99.html both returning the same resource. This is technically duplicate content - although is not necessarily a problem providing you ensure that the /article99 URL is the only URL that is ever referenced. As a minimum you should set a <link rel="canonical" element pointing to the canonical URL, ie. /article99.

1
  • Thanks for your detailed answer. From the sounds of it enabling multiviews will be appropriate on my site, since a) it's a very simple collection of static .html articles with a handful of image files... and b) because 99% of the external links are of the clipped <a href="article99" variety (I know, I know). Multiviews should get everything to open properly, and there's nothing fancy I can see it breaking.
    – Chris M
    Sep 14 '17 at 1:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.