2

I wanted to include PHP in my css files, but I don't want to go site-wide and change the URL. Is it possible, using HTACCESS, to serve style.php when style.css is requested? Here's what I tried so far:

RewriteRule /_css/style.css /_css/style.php  

Or, better yet: Allow me to keep the extension but have HTACCESS treat it like a PHP file? I'm not very knowledgeable with HTACCESS. Thanks for the help.

1
  • i think you neeed to turn the rewrite engine on RewriteEngine on Commented Mar 11, 2014 at 19:00

4 Answers 4

5

You can do it this way

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^_css/([^/]+)\.css$ /_css/$1.php [L]

This rule will match every url like /_css/FILENAME.css if this is not an existing css file (only matching virtual files). If matched, then rewrites to /_css/FILENAME.php

Also, don't forget to enable mod_rewrite if not already done

Sign up to request clarification or add additional context in comments.

3 Comments

@Shane Crystal's answer suits you better
@nl-x No it does not. See the beginning of the question: I wanted to include PHP in my css files, but I don't want to go site-wide and change the URL
@JustinIurman Ah, sorry. Didn't understand what you meant. Thought you didn't want to side-wide change your php settings. So then just take a look at my answer that I just posted. Still no need for htaccess usage.
3

Just put this in your file.css at the very top:

@import url("file.php");

Comments

1

Url rewriting is needless here. You can call your PHP script this way:

<link rel="stylesheet" href="/_css/style.php" type="text/css" />

This is perfectly valid and does not slow you down.

1 Comment

But they don't want to go site-wide and change the URL
0

Add this code to your htaccees file.

AddType application/x-httpd-php .css

but it is not recommended because your server may slow.

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.