Shane
Shane

Reputation: 1230

Serve 'file.php' when 'file.css' is requested

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.

Upvotes: 2

Views: 267

Answers (4)

nl-x
nl-x

Reputation: 11832

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

@import url("file.php");

Upvotes: 3

Justin Iurman
Justin Iurman

Reputation: 19016

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

Upvotes: 5

Ekramul Hoque
Ekramul Hoque

Reputation: 5123

Add this code to your htaccees file.

AddType application/x-httpd-php .css

but it is not recommended because your server may slow.

Upvotes: 0

vaultah
vaultah

Reputation: 46533

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.

Upvotes: 1

Related Questions