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]
Related Posts
- PHP: Run HTML as PHP
- 12 Rocking Apache .htaccess Tutorial Examples, Tips – Commands & Directives
- .htaccess: Directory Listing – Enable Web Directory Browsing & Indexing
- PHP: Why you should use dirname(__FILE__).‘/include.php’ instead of just ‘include.php’
- Set Expiration or Expiring Time by mod_expires.c on Apache via .htaccess to Reduce Web Page Loading Time

{ 1 comment… read it below or add one }
Can you please go into the rational / benefits of using the mod_rewrite method.
I’d really appreciate it!