Apache htaccess results in files being downloaded instead of displayed

9,835

Browsers offer to download files based on two headers:

I use a command line tool called curl to check headers on a website.

curl -s -D - 'http://example.com/' | head -n 20

If you don't want your pages to be downloaded, then the Content-Type should be set to text/html or text/html;charset=UTF-8 and there should be no Content-Disposition header.

Browsers will offer to save pages as a download if the Content-Type header is something like application/octet-stream. A download dialog box will also be presented if there is a Content-Disposition header such as attachment; filename=foo.html.

You can generally control the Content-Type through .htaccess using the AddType directive like such:

AddType text/html .html

I see that you are already doing this for .shtml files. You may have to do the same for other extensions such as .html and .htm.

This is then the default type for files with those extensions, but if they are backed by a web application, the code that runs that web application usually has the ability to override them. Most web application frameworks have some sort of setHeader directive that can be used to set both Content-Type and Content-Disposition. In addition to checking your .htaccess file, you will need to check the code that powers your website.

Share:
9,835

Related videos on Youtube

chrissik
Author by

chrissik

Updated on September 18, 2022

Comments

  • chrissik
    chrissik over 1 year

    So I had this "beautiful" website that did exactly what I wanted it to do. Then I shut down my PC, reboot and...the pages just download now instead of being displayed.

    I re-installed XAMPP and launched Apache again and I was able to identify the .htaccess file as the cause of the problem.

    Options +FollowSymlinks
    RewriteEngine on 
    
    RewriteCond %{QUERY_STRING} !^desktop
    RewriteCond %{HTTP_USER_AGENT} "android|blackberry|googlebot-mobile|iemobile|iphone|ipod|#opera mobile|palmos|webos" [NC]
    RewriteRule ^/?$ /mobile/index [L,R=302]    
    RewriteRule ^/?$ /de/index [R]
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^(.*)$ $1.html
    

    Here is the problem I guess:

    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^(.*)$ $1.html
    

    This should make it possible to use /de/index instead of /de/index.html - but somehow it causes the page to download if I open localhost/de/index (but with localhost/de/index.html it works fine...).

    I'm using HTML Sites with SSI Elements on a Apache web server. The only other file that is different to the out-of-the-box ones is the httpd.conf, where I enabled SSI:

    AddType text/html .shtml
    AddHandler server-parsed .shtml
    AddHandler server-parsed .html
    AddHandler server-parsed .htm
    Options Indexes FollowSymLinks Includes
    AddOutputFilter INCLUDES .shtml
    Options +Includes
    

    So I hope there is somebody among you that can help me with this annoying problem as I'm quite desperate... for some reason, even without the problematic lines Chrome keeps downloading the files (even if I delete the .htaccess file), while IE and Opera display the pages.

    Edit: Now Opera also wants to download files (whether index.html or index are called).

    • MrWhite
      MrWhite over 10 years
      When you say "downloading the files", is the browser prompting with a "Save As..." dialog? To be honest I can't immediately see anything in your .htaccess that would cause this, and if the browsers still download after having removed these lines then it's not conclusive - although that might be a caching issue. If the browser is prompting to download then there could be a problem with the Content-Type headers (you can check this in the browser). Just to clarify, is there just HTML and SSI involved, no other server-side scripting?
    • Simon Hayter
      Simon Hayter over 10 years
      Sounds like a problem with the MIME types, if you have php5 installed you could do a php info file and see whats operating.
    • chrissik
      chrissik over 10 years
      qw3d: In IE there's the 'save as' dialog, in Chrome etc. it just saves the file. There is nothing else than HTML and SSI involved. I tried clearing the cache but that didn't change a thing. Apart from the one change in the config there are no other changes in my xampp-setup
    • MrWhite
      MrWhite over 10 years
      (In Chrome's settings you would need to set it to prompt, otherwise it downloads automatically to the default location.) As stated above, this does sound like a mime type issue - have you checked the Content-Type headers in the response?
    • chrissik
      chrissik over 10 years
      First of all thanks to all you guys offering me a solution. As sometimes (more often than one would like) the best solution - or the one that helped me - was to remove and reinstall the hole software things (XAMPP, Browsers and everything else) and install them again And it worked... So I don't have any idea why it didn't work. Maybe some wrong configuration, cache, registry entry, config file, whatsoever So again a huge thank you to you guys, but this hard method helped....