jskidd3
jskidd3

Reputation: 4783

Include PHP inside CSS file without PHP extension

I understand that I can name a file main.css.php and then set headers inside the file like so:

<?php header("Content-type: text/css; charset: UTF-8"); ?>

I can then dynamically set my styles and I have this working fine.

However, I would like to achieve the same effect with the name of the file simply being main.css, without exposing the PHP extension. How can I go about achieving this? The server is running Apache 2 on Ubuntu.

Current HTML: <link href="main.css.php">
Desired HTML: <link href="main.css"> (still includes PHP)

Upvotes: 4

Views: 183

Answers (2)

ŽaMan
ŽaMan

Reputation: 396

This is just for apache2 and php? Then one idea would be to interpret the .css as a .php which can be done by editing the .conf that includes the php extension or find this line somewhere in the config files:

AddType application/x-httpd-php .php

and edit to show

AddType application/x-httpd-php .php .css .phtml .etc .etc

Upvotes: 2

Neodan
Neodan

Reputation: 5252

You can use the mod_rewrite for it. Create the .htaccess file in your css directory and put this code:

RewriteEngine on
RewriteBase /your_css_dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.php [L]

Upvotes: 0

Related Questions