最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to make certain javascript files render from php using htaccess - Stack Overflow

programmeradmin1浏览0评论

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

.js

Would then be sent to something like:

.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!

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./loads/923as0d9f89089asd.js

Would then be sent to something like:

https://www.domain./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!

Share Improve this question asked Aug 14, 2016 at 17:26 MrTechieMrTechie 1,8476 gold badges21 silver badges36 bronze badges 3
  • redirect them to a different php file and serve them from there – Akshay Khandelwal Commented Aug 14, 2016 at 17:31
  • Have you already looked at the htaccess concepts of Rewrite and Redirects? – KumarM Commented Aug 14, 2016 at 17:32
  • This might help you: htaccesscheatsheet./#rewrite-and-redirection. I has a lot of examples. – KumarM Commented Aug 14, 2016 at 17:36
Add a ment  | 

2 Answers 2

Reset to default 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.

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

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/loads/(.*).js$
RewriteRule ^/loads/(.*).js$ http://www.domain./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/docs/current/rewrite/flags.html#flag_p

发布评论

评论列表(0)

  1. 暂无评论