Discussion Locked

Displaying Text File In Apache

0 helpful
7 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I have a text file (with extension .txt) that contains HTML source codes. Whenever i click a link to my text file, instead of getting the contents of the file, the HTML codes are executed. How can I prevent this from happening ? I suspect this has something to do with mime_magic configuration in my Apache Server, but I am not sure exactly where to change it.

7 replies

by cactus pete

The server isn't causing this, the client interpretting the file is.

To force the client to see it as preformatted text, put

 at the beginning of the file, and 

at the end.

by paulo.sedrez

Search the configuration file for mime_magic; you will find in two lines:

LoadModule mime_magic_module modules/mod_mime_magic.so

AddModule mod_mime_magic.c

Comment out this lines. Then search for the configuration clause, MIMEMagicFile:MIMEMagicFile conf/magic

or similar. Comment out this line. This will prevent identification of files based on its contents.

Now check the DefaultType. It should be 'text/plain'. Most systems change it to 'text/html' to make work bad CGI scripts.

If you want to be sure that .txt files are allways treated as text, you should use AddType:

AddType text/plain .txt

--Sedrez

by shiva

Try viewing the file with different browsers. Internet Explorer will sometimes ignore a file's MIME type and interpret HTML markup. Mozilla and Opera are stricter in their interpretation of the data stream.

You can also use a tool like wget or even telnet to view the raw HTTP transaction and see whether the server is reporting the correct MIME type, which is ultimately what *should* be used to determine how to render the file.

This discussion is closed to new replies.