Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make certain javascript files render from php using htaccess

So I haven't quite figured out the proper structure to use within my .htaccess file for what I am looking to achieve.

What I would like to have happen is any javascript files that are called from a folder (and only that folder) are generated by a php file. I'm not looking to have a .php extension or create multiple files for this.

An example is

https://www.domain.com/loads/923as0d9f89089asd.js

Would then be sent to something like:

https://www.domain.com/loads/js.php

Not looking to use a $_GET method on the php either. Basically the php file detects the file name and then does what it is supposed to do from there.

What would be the best way to set this up within an .htaccess file?

Thanks!

like image 564
MrTechie Avatar asked Aug 14 '16 17:08

MrTechie


1 Answers

Inside loads/.htaccess you can use this rule:

RewriteEngine On
RewriteBase /loads/

RewriteRule ^[\w-]+\.js$ js.php [L,NC]

Inside your js.php you need to use $_SERVER["REQUEST_URI"] to get original JS filename and serve the content.

like image 117
anubhava Avatar answered Oct 25 '22 11:10

anubhava