6

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!

3
  • redirect them to a different php file and serve them from there Commented Aug 14, 2016 at 17:31
  • Have you already looked at the htaccess concepts of Rewrite and Redirects? Commented Aug 14, 2016 at 17:32
  • This might help you: htaccesscheatsheet.com/#rewrite-and-redirection. I has a lot of examples. Commented Aug 14, 2016 at 17:36

2 Answers 2

5

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.

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

3 Comments

That's pretty close. When I use /loads/jaklsdjfkljklasdf.js I get a not found, but if I do /jaklsdjfkljklasdf.js it does in fact allow me to get the url in the /loads/js.php file. Was hoping to have it come from the /loads/ directory and not just be served from top level (ie: /loads/jojklajklsdjfkl.js and not /jaklsdjfkljaklsdf.js)
If you place above rule in loads/.htaccess as I suggested in answer then it will indeed work for /loads/jaklsdjfkljklasdf.js.
Works perfectly! Thanks so much.
2

You could have something like this (untested, may need modification) :

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/loads/(.*).js$
RewriteRule ^/loads/(.*).js$ http://www.domain.com/loads/js.php?file=$1 [P]

And your js.php script will be called internally, it will receive the JS filename from GET but it's not visible by the user.
This will use mod_proxy with the [P] flag.
More informations here : https://httpd.apache.org/docs/current/rewrite/flags.html#flag_p

4 Comments

Not looking to have it redirect to the php file at all. Just want the js reference to be handed off to the php file and the php file render the js.
My exemple code will do an invisible redirect, your user's browser will see for example this url : www.domain.com/loads/myfile.js
No, your example actually redirects them to the php file and ends up on js.php?file=xxxx
Try to have mod_proxy activated (a2enmod mod_proxy) and use the [P] flag instead of [L], more info here : httpd.apache.org/docs/current/rewrite/flags.html#flag_p

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.