views:

43

answers:

3

If I have a very simple http directory:

default.html
info.html
contact.html
etc...

Should default.html link to the other files in the directory, I've always been able to simply use an anchor tag thus:

<a href="contact" target="_self">Contact me!</a>

Will this always work, assuming that there is only one file in the directory with a name matching this extension-less href value?

+2  A: 

There is a content negotiation feature in Apache2 which does that, but personally, I do not like to rely on that.

If I need nice URLs, I'd better use mod_rewrite and implement completely custom url scheme which would be easy to modify & customize without limits.

http://httpd.apache.org/docs/2.0/content-negotiation.html

BarsMonster
+1  A: 

No it does not automatically appends .html as he could not know which file extension to use. Let's say you have a contact.html and a contact.php. Which one should he use.

However you can do all this using rewrite rules (e.g. in a .htaccess file). Just search for some examples here on SO or in the web.

Kau-Boy
I edited the question to clarify what I'm really asking. Thanks.
Isaac Lubow
+3  A: 

It depends on the server and how it is set-up.

Remember that there's no innate mapping between URIs and files on a webserver, the webserver is always following some sort of rule as to what file to send. The simplest takes the path part of the URI and does a direct mapping to a filepath local to the webserver, but it could be doing just about anything else. A common case is using the file extension to do con-neg, so if you have contact.html and contact.atom and so on in the same local directory corresponding to the path, it picks that closest to the Accept header from the user-agent.

Putting file extensions (whether of "static" files or handlers like .php, .aspx, etc.) in URIs is rather pointless since there is no such thing as a file on the web (there are files on the server, and the client can save the stream to a file, but on the web itself there are octet streams that may or may not correspond to a file). And less than ideal; presumably contact.html has something to do with contact details, while "contact" expresses this idea well, ".html" has nothing to do with contact details and doesn't belong there.

Hence the more sensible URI would not have ".html" in it, unless this was in some way expressing something useful (such as explicitly asking for a HTML version and bypassing content-negotiation, or if the page was actually about HTML).

On the other hand, just mapping directly to file names is a quick and easy way to do things, so while I certainly frown on such arbitrary cruft in URIs I won't jump through too many hoops not to use it, especially in secondary URIs used for stylesheets, images etc. rather than those which are expected to regularly appear in the address bar of a browser.

On the third hand, once you remove such cruft, adding more sophisticated handling later if required, becomes a much easier transition.

Jon Hanna
You seem to know a lot on the subject and I would defer to such knowledge for an answer, but alas I still have no idea how you feel about it. When you say 'sensible', do you mean philosophically sensible? Or practical? Presently, I'm asking for the practical solution.
Isaac Lubow
Philosophically, ".html" has no business being in a URI that doesn't have anything to do with HTML in and of itself. Practically is covered by the last two paragraphs - on the one hand it can be easier (depending on set-up) to keep the file extension in, but on the other hand if you do want to transition to something more complicated than straight file mapping, it'll be easier if you hadn't. Since in your case leaving out the ".html" rubbish is easy, then both practically and philosophically I'd say to leave it out.
Jon Hanna