.css file using php

Hello,

I would like a .css file in which I can use php. I’ve searched around for the simplest solution, and found various ways of doing it. Decided toa sk the gurus.

Regards,

-jj. :slight_smile:

Using mod_rewrite you could do


Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteRule ^(.*)\\.css$ $1.php [L]

Then when you request <something>.css, Apache will serve <something>.php instead :slight_smile:

Many thanks for the reply :slight_smile:

I’d rather keep the .css extension, but enable php within it. Is that something you can do?

Yes you can do that if you know the name of the PHP handler within Apache.
Usually it would be something like


AddHandler application/x-httpd-php .css

or


AddHandler application/x-httpd-php5 .css

The name of the handler is dependent on the host, but application/x-httpd-php and application/x-httpd-php5 are the most common names.

Perfect!

Thanks you very much :slight_smile:

Do I have to send the appropriate header()?

Yes you do:


Header('Content-Type: text/css');

Otherwise older browsers may cop out

Is it ok if it’s at the very bottom of the file?

No it should be at the very top before you output anything.

Can use both handlers for compatibility or should I make sure that I’m using the correct one?

You should make sure you’re using the correct one. Probably just one of them works (or neither work). It all has to do with how your host set stuff up. If neither work, please contact your host for assistance.