Forum Moderators: phranque
I am facing a peculiar problem. I have created html files without extension like domain.com/abc which should be rendered as html to the client browser. I am able to send the header to the client to treat such files without extension as html.
But I would like to render such files as shtml so that they are able to parse server side includes. Now how do I do this?
Another issue is that if I set it like this then how would the server understand whether the file in question is a file or a directory, in case it encounters one.
I have heard there is certain force-type handler which might be possibly be able to solve this issue.
A path with a trailing slash refers to a directory -- more specifically, to a directory index (table of contents) listing.
The DirectoryIndex directive may be used to map a directory index URL to a file.
URLs and files are not the same thing. They are two different ways to refer to resources. Browsers use URLs to request desired resources via HTTP, while servers translate URLs into *nix or Windows filepaths to retrieve content, or into script invocations to generate content.
With the above in mind, a simple approach is to name your files to reflect their real types -- i.e. .html, .shtml, .txt, .css, etc., and then use mod_rewrite to map whatever type of URLs you want to point to those files.
You can even use "file-exists" checking to map extensionless URLs to different filetypes based on whether a file exists when the most-likely extensions are added to the requested extensionless URLs. This can be done manually using mod_rewrite, or automatically using content negotiation/MultiViews. An example mod_rewrite might be:
# If file exists when .shtml is appended to extensionless URL, then serve .shtml file
RewriteCond %{REQUEST_FILENAME}.shtml -f
RewriteRule ^([^.])$ /$1.shtml
You will observe that on Apache, MIME-types are set by file type, and the URL used to refer to those files makes no difference in the MIME-type sent with the server response.
Hopefully, this will at least clarify the issue a bit.
Jim
I have renamed all the files without extension as filename.shtml and i will tried out the code that you gave but it is still not working for me.
init rewrite engine is requesting the url, applying pattern, and it says pass, but when i request the filename.shtml without extension via browser it gives a 404
[httpd.apache.org...]
[httpd.apache.org...]
You may need to add
Options +MultiViews to your root-level .htaccess (or httpd.conf) but if content negotiation is available it can handle this situation out of the box:. Extensionless URLs are resolved to the most appropriate resource, be it a .html file, .php, .shtml,... and the files are parsed according to their extension.
I mentioned content-negotiation and MultiViews above. I like to avoid these for simple applications, as their use can sometimes cause other unexpected side effects. The way I think of it, the mod_rewrite solution can be used as a 'point solution' for simple cases like this one, while content-negotiation/MultiViews requires a 'full investment' in the site's support of content negotiation. There are few usability issues worse than visitors unexpectedly getting the "Multiple versions exist" message from mod_negotiation. Also, using MultiViews can affect network cacheability.
Jim