3

I have multiple css files in a directory (style0.css, style1.css etc..) How can I redirect a request to these css files with htaccess so that a php can process the requested css file. ex. /styles/style0.css -> /includes/compressor.php?i=style0.css ?

2 Answers 2

7

Add an .htaccess to your css directory with these two lines. You can now add php code to your css files, and it will be processed :

AddHandler application/x-httpd-php .css

php_value default_mimetype "text/css"
Sign up to request clarification or add additional context in comments.

3 Comments

Better to use rewriting if possible, since a .css file should contain CSS, not PHP. It's semantically incorrect and annoying to edit, as your editor will try to parse the file as CSS and fail.
i'm using it in the following way: my css is singleSheet.css?sheets[]=main.css&sheets[]=rest.css this allows me to have one sheet, built server side (no @includes)
I think this is a good idea too and I might use it in the future so +1, but because I only want to compress the css files with php and stuff like that, I would have to copy the same code to all my css files and if I would want to change something I would have to edit all of them.
6
RewriteEngine On
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]

These .htAccess command activate the RewriteEngine. This will let you analyse the request URL and execute the one you want into the server. The second line take all URL that end with .css and will take the file name to insert it as a parameter (like you wanted in your example).

For example:

http://localhost/styles/style1.css will go to

http://localhost/includes/compressor.php?i=style1.css

The L flag will tell Apache to stop processing the rewrite rules for that request.

2 Comments

would you explain a bit please? what does the [L] do?
The L flag will tell Apache to stop processing the rewrite rules for that request.

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.