Welcome to WebmasterWorld Guest from 54.226.146.15

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Render Files without Extension as Shtml

     

Imaster

4:28 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi,

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.

jdMorgan

4:48 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



A path without a trailing slash refers to a file.

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

With a series of tests like this eaxmple, you can prioritize by most-likely filetype first. If a request fails all filetype tests, then append a slash and try again as a directory index request.

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

Imaster

6:23 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks a bunch Jim. I will try it out.

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

encyclo

6:47 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member encyclo is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Can you use content negotiation on your server?

[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.

jdMorgan

7:48 pm on Nov 1, 2006 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



If your rewrite log says "pass-through", then that is bad, not good. It means the requested URL did not match the Rewrite pattern, and therefore, no rewrite has taken place.

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

 

Featured Threads

Hot Threads This Week

Hot Threads This Month