4

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)

8
  • 1
    generate the css and css file via php Commented Nov 8, 2015 at 18:05
  • A dirty hack you could use if placing in the PHP file in main.css/index.php and then linking to the file/folder as main.css. Commented Nov 8, 2015 at 18:06
  • 1
    @Chris Well yes, as I said in the question I'm already generating the css via php. My question asks how to generate the css without referencing a file with the php extension... Commented Nov 8, 2015 at 18:08
  • generate the file as well... Create a new css file, write your generated css to it... Commented Nov 8, 2015 at 18:10
  • @Chris Could you please explain further what you mean by generate the file? Commented Nov 8, 2015 at 18:12

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

Comments

0

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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.