Categories
.htaccess Tutorials & Tips PHP Tips & Tutorials

PHP: Run .CSS files as PHP

Most of time we need to run some other mime types as PHP in Apache web server, such as run PHP code in HTML files, we want to Apache to parse that particular type of file so that PHP code in them are recognized and executed. Though from the external viewers’ point of view, such as Google bot or human visitors, nothing’s different.

To run .css files, the cascading style sheets as PHP or enable the PHP code in .css files so as to harness the power of a programming language in rendering and supplying the stylesheet file, just add a simple line in the .htaccess file of the particular directory where you are hosting those .css files containing PHP code:

AddType application/x-httpd-php .css

That’s all. And in case you want to make the entire web document directory to run .css files as PHP once and for all, better don’t do it because Apache would demand unnecessary extra resources to parse all .css files, containing or not containing the PHP code.

Update: A better approach is to rely on mod_rewrite the Apache URL rewriting module to rewrite any incoming requests for .css files to corresponding .php files.

RewriteEngine On
RewriteRule ^screen\.css$ screen.css.php [NC]

By Yang Yang

Hello, I'm Yang. I build online businesses that please people. Want to join in and post some useful articles on Kavoir.com? Shoot me a message.

2 replies on “PHP: Run .CSS files as PHP”

But now you have a problem because your .css files will be delivered with PHP default mime type text/html instead of text/css, unless you stick a Header() line at the top of each PHP file to fix it. This is no matter what AddType or mime.magic you have going in httpd.conf. I think.

Comments are closed.