简体   繁体   English

如何配置apache使用php创建一个javascript文件

[英]How can I configure apache to use php to create a javascript file

I want clients to be able to include one of my javascript files on their site like this: 我希望客户能够在他们的网站上包含我的一个javascript文件,如下所示:

<script src="http://www.example.com/blah.js" ></script>

But I want to actually use php to dynamically create the javascript file/response (I don't want blah.js to actually exist on my server). 但我想实际使用php动态创建javascript文件/响应(我不希望blah.js实际存在于我的服务器上)。 I am running ubuntu 14.04 and apache 2.4.7. 我正在运行ubuntu 14.04和apache 2.4.7。 blah.js is the only file I want my server to act on like this. blah.js是我希望我的服务器像这样做的唯一文件。 In my php I will check the IP address and create the javascript file/response based on the client site. 在我的PHP中,我将检查IP地址并根据客户端站点创建javascript文件/响应。 I want any other javascript file request to act as normal. 我希望任何其他javascript文件请求正常运行。

Can I use .htaccess to override the directory to have any request for blah.js to be handled by php? 我可以使用.htaccess覆盖目录以获得由php处理的blah.js的任何请求吗?

Use rewrite in your htaccess : 在你的htaccess中使用重写:

RewriteEngine On
RewriteRule ^blah\.js /blah.php [L]

When you will call /blah.js, it will be redirected to blah.php 当你打电话给/blah.js时,它会被重定向到blah.php

You can also use .htaccess auto_prepend_file directive to prepend .php file before any of Your other php files come into play, and than use that prepended script to generate whatever You might need. 您还可以使用.htaccess auto_prepend_file指令在您的任何其他php文件发挥作用之前添加.php文件,而不是使用该前置脚本生成您可能需要的任何内容。

In Your .htaccess file: 在.htaccess文件中:

php_value auto_prepend_file "/my/loc/to/php/file.php"

There is also auto_append_file which will pull particular script just after last one manually included by You or application, and do the job after all content is generated, before headers are sent to the client. 还有auto_append_file ,它将在您或应用程序手动包含的最后一个之后拉出特定脚本,并在生成所有内容之后执行作业,然后将标头发送到客户端。

Note that php_value directives are not enabled on each shared hosting setup. 请注意,每个共享主机设置都未启用php_value指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM