Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

.htaccess

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.

like image 796
Shane Avatar asked Dec 28 '25 10:12

Shane


1 Answers

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

like image 176
Justin Iurman Avatar answered Dec 30 '25 22:12

Justin Iurman