8th Grader 8th Grader - 7 months ago 13
SQL Question

Detect if php file is being used in <link> or <script> tag

I have a PHP file called

styles.php?id=1
. This fetches CSS code from MySQL and using
Content-type: text/css
. It can be used with
<link>
tag.

If I use
.htaccess
to rewrite engine, then it would be
/styles/1
. I have a similar file which fetches js (
/scripts/1
) code instead.

Instead I want one file called
code.php
which can detect which tag it is being used in and fetch the code from the database.

Answer

It has been quite a while but I got an answer. Just thought to share it with other people who need something similar to this.

In .htaccess

RewriteEngine On
RewriteBase /

RewriteRule ^file/(.*)\.css$ Home/file.php?id=$1&type=css
RewriteRule ^file/(.*)\.js$ Home/file.php?id=$1&type=js

And In PHP, I would check if the parameter type is equal to css or js and set the content-type header