Kevin Loney
Kevin Loney

Reputation: 7553

Alternatives to PHP header function

I'm working on a website that has a number of style sheets all of which need to be handled as PHP scripts server-side. My .htaccess file looks something like this:

<FilesMatch "\.(css)$">
    ForceType application/x-httpd-php
</FilesMatch>

This causes a small problem as the mime type of the http-response's Content-Type field is then set to text/html instead of text/css.

Obviously I can fix this by adding header('Content-Type: text/css') to all of my files but is there a better way?

Can I do this from within the .htaccess file? None of the directives offered by mod_mime or mod_negotiation seem to be what I'm looking for.

Upvotes: 4

Views: 1093

Answers (2)

Cal
Cal

Reputation: 7157

try:

php_value default_mimetype "text/css"

Upvotes: 8

Hexagon Theory
Hexagon Theory

Reputation: 44803

You should be able to just add a .php extension to all of your CSS files; this will cause the server to pass all of the "normal" CSS as just that, whereas all of your dynamic, PHP-generated CSS should be parsed. After that, it's just a matter of slightly modifying the way in which you link your stylesheets:

<link rel="stylesheet" type="text/css" href="styles.css.php" />

Though that's always done the trick for me, there's a slight chance you may need to explicitly declare the MIME type of your CSS files using PHP's header() function, like so:

header('Content-type: text/css');

Upvotes: 1

Related Questions