Apache always returning js as text/plain - apache

I have an Apache install which always returns .js files as text/plain.
I've disabled mod_negotioation, and checked the MIME is set in /etc/mime.types. I know this is the correct file, as I added application/font-woff, which is working as expected.
I've also disabled mod_pagespeed, and renamed /etc/apache2/magic to /etc/apache2/magic.disabled, even though mod_mime_magic isn't enabled.
Even adding AddType application/javascript .js to the .htaccess results in the same text/plain.
Where else can I force the type to be set?

Use the other JavaScript mime types:
AddType application/x-javascript .js
#AddType application/x-ecmascript .js
#AddType application/ecmascript .js
#AddType text/javascript .js
#AddType text/ecmascript .js
Try one at a time until you find a match.
References
RFC 4329: Scripting Media Types
Reducing MIME type security risks (Windows)

Related

Apache Set MIME Type Dynamically

We have mutiple files where we need to set MIME type based upon file type. Ex:
/{path}/alpha.woff --> font/woff
/{path}/beta.woff2 --> font/woff2
/{path}/gamma.ttf --> font/ttf
We have added the below snippet in apache config file but not certain how to achieve with one LocationMatch attribute
<LocationMatch "\.(?i:woff|woff2|ttf)$">
Header set Content-Type font/{dynamically set woff OR woff2 OR ttf }
</LocationMatch>
This can be done with multiple LocationMatch (one for each font) but looks not optimized one.
Can anyone suggest better solution to deal with such situation?
Tried with regex which throw an error.
<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))$">
Header set Content-Type font/woff // Work fine but set font/woff for all fonts
</LocationMatch>
Below throw error in config
<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))$">
Header set Content-Type font/%{env:MATCH_FONTTYPE}
</LocationMatch>
Unrecognized header format %
The mime types can be configured in the conf/mime.types configuration file of the Apache HTTP Server, see the example here:
font/woff woff
font/woff2 woff2
font/ttf ttf
If you can't update this file, and LocationMatch directive is your only option, then starting from Apache 2.4.8 you can use named regex capture groups, see documentation.
In the following example, I've named the capture group fonttype, so by convention it will be put into the environment variable MATCH_FONTTYPE. This environment variable can be used in the ForceType expression:
<LocationMatch "\.(?<fonttype>(?i:woff|woff2|ttf))$">
ForceType font/%{env:MATCH_FONTTYPE}
</LocationMatch>

My scripts and styles get served as text/html

Since the update to Mac OS Mavericks all my styles and scripts get served as text/html
I have no idea where I can change this behavior. I'm using the built in apache service
EDIT: I forgot to mention that static files works as expected. The files above get parsed by a php script to get served cached and compressed. I'm using header("Content-type: text/css") to define the content type
In your server/vhost config try adding:
AddType text/javascript .js
AddType text/css .css
If something else is already setting the type, you can try adding this instead:
<Files "*.js">
ForceType text/javascript
</Files>
<Files "*.css">
ForceType text/css
</Files>
After further digging into the cacheing script I found a flush() method forces PHP to add a text/html content type in the header. This happens right before I add headers like header("Content-type: text/css")
I've rewritten my class method and the output is now correct. Strange is that it doesn't cause the problem on my live server with the same cacheing class

Why does Gzip compress my css & javascript but not my HTML/php?

When I check this website: http://www.tropicbreeze.co.uk/, for example with http://checkgzipcompression.com/ - it reports that it is using Gzip. But Yslow disagrees.
I have this in my .htaccess file:
AddOutputFilterByType DEFLATE text/html text/php text/x-php application/php application/x-php application/x-httpd-php-source text/plain text/xml text/css text/javascript application/x-javascript application/xhtml+xml application/javascript application/x-httpd-php
Checking in the Net tab of Firebug for headers, I can see that the various associated .css and .js files on that page have Content-Encoding gzip appearing as expected - but the php files do not.
Yslow tells me that the homepage is not using Gzip. The Firebug Net tab says that the home page (and other php files on the server) are not being sent with Content-Encoding gzip
I've tried adding all the mimetype filters I could find suggested, and so far as I can see, DEFLATE text/html should cover it anyway, but still no joy.
I have cleared my cache and am not using a proxy.
Can anyone suggest what I've missed? Why are the php files not being gzipped when the other files are? Or, if they are being gzipped, why does Firebug /yslow think they aren't?
Not sure exactly what the issue might be but perhaps an easy fix would be to try something like...
<IfModule mod_deflate.c>
<FilesMatch "\.(css|htm|html|js|php|txt|xml)$">
SetOutputFilter DEFLATE
</FilesMatch>
</IfModule>
...which should setup compression on popular file extensions including PHP.
I personally think this format is much easier for a human to read too. Bonus!

htaccess setting the mimetype for a single file

In an .htaccess file one might set the mimetype for a given extension like:
AddType application/javascript .js
How would one set the mimetype for a single file instead of an extension? I have one Javascript that needs a different mimetype than other Javascript files in the same folder.
AddType application/javascript .js
AddType application/ecmascript specialfile.js
The second line does not work.
You can use the <files> container to match the file's name before adding a type for it:
<Files specialfile.js>
AddType application/ecmascript .js
</Files>

Apache AddType text/css .css Ignored

I have made an ,htaccess file in my /font directory with only this line in it (as part of troubleshooting).
AddType text/css .css
None the less Google Chrome (even 19.0.1053.0 canary) reports that the .css files are returned as: MIME type text/html
http://paulanorman.com/:11 Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://paulanorman.com/fonts/iwona-stylesheet.css".
I use Hostgator and have checked in Cpanel that text/css is default on my server for .css
I have also checked using wget on cygwin bash shell and the documents are indeed coming down as text/html
These stylesheets are being ignored. However I have found if I
<style ..>
<?php include "dah dha / style-sheets.css" ?>
</style>
one by one in the head of my document all is fine.
However I would like not to have to do that.
Any one solved this? I have read much all over the net about this and it appears as an unexplained fickle problem it seems.
TIA
paul
According to www.askapache.com this is ok as a one step process with the quote marks
AddType 'text/css; charset=UTF-8' css