简体   繁体   English

让Apache / PHP根据文件扩展名设置MIME类型,以将文件作为PHP执行

[英]Have Apache/PHP set MIME type based upon file extension for files being executed as PHP

I have an Apache/PHP based server on which I've made Apache execute js , html and css files as PHP via the following line in /etc/mime.types : 我有一个基于Apache / PHP的服务器,通过/etc/mime.types的以下行,使Apache在PHP上执行jshtmlcss文件作为PHP:

application/x-httpd-php phtml pht php js html css application / x-httpd-php phtml pht php js html css

We did this because even though any files we save as .html / .js / .css are almost entirely non-PHP, we need to do a bit of templating in them (for example, changing the domain of some URLs depending upon whether we are on the live or testing server). 我们这样做是因为,即使我们保存为.html / .js / .css任何文件几乎完全不是PHP,我们也需要在其中进行一些模板化(例如,根据我们是否更改了某些URL的域)在实时或测试服务器上)。

The trouble is, this method of making Apache treat those files as PHP causes them to be returned with the wrong MIME type, which at the very least breaks css files in Chromium and probably has nasty effects in other browsers too. 问题在于,这种使Apache将这些文件视为PHP的方法会导致它们以错误的MIME类型返回,这至少会破坏Chromium中的css文件,并且在其他浏览器中也会产生讨厌的影响。

Is there a way I can tell Apache to execute these files as PHP but still output them with the correct MIME type for their file extension? 有没有一种方法可以告诉Apache将这些文件作为PHP执行,但仍使用文件扩展名的正确MIME类型输出它们? I don't want to have to manually paste 我不想手动粘贴

<?php
  header("Content-type: text/css");
?>

at the top of all of our .css files. 在我们所有.css文件的顶部。

We did this because even though any files we save as .html/.js/.css are almost entirely non-PHP, we need to do a bit of templating in them 我们这样做是因为,即使我们保存为.html / .js / .css的任何文件几乎完全不是PHP,我们也需要在其中进行一些模板化

Why not do the dynamic parts in the main document then, where you are dynamic anyway? 那么为什么不在主文档中创建动态部件呢? Running all the resources through the PHP interpreter is such a waste. 通过PHP解释器运行所有资源非常浪费。

For JavaScript: 对于JavaScript:

<script>
domain = "<? echo $domain; ?>";  <----- dynamic bits here
</script>
<script src="xyz.js"></script>   <----- you can use them in xyz.js

For CSS: 对于CSS:

<link rel="stylesheet" href="stylesheet.css">

<style type="text/css">
 /* insert dynamic CSS here, overriding parts from the static style sheet */

 .my_div { color: <? echo $color; ?> }

</style>

It seems worth me noting that the only reason I ever ended up needing to ask this question was the wrong-headed approach I'd taken to having .js and .css files executed by PHP. 似乎值得我注意的是,我最终不得不问这个问题的唯一原因是我对PHP执行.js.css文件采取了错误的方法。

Don't modify your mime.types file to achieve this. 不要修改您的mime.types文件来实现此目的。 Instead, in your Apache config (or in a .htaccess file), use invocations like this: 相反,在您的Apache配置(或.htaccess文件)中,使用如下调用:

<Files *.js>
    ForceType application/x-httpd-php
    Header set Content-Type "application/javascript"
</Files>
<Files *.css>
    ForceType application/x-httpd-php
    Header set Content-Type "text/css"
</Files>

You can do it in two ways: 您可以通过两种方式进行操作:

  1. Create a .htaccess file. 创建一个.htaccess文件。

    Now inside this file, make all the PHP files to be read as either .js or .css . 现在在此文件中,将所有PHP文件都读取为.js.css Ideally, you will be having files like: style.css.php , scripts.js.php and are executed by Apache but the user will be getting the URL as style.css , scripts.js , etc. 理想情况下,您将拥有如下文件: style.css.phpscripts.js.php ,并且由Apache执行,但用户将获得的URL为style.cssscripts.js等。

  2. Using Mime types . 使用Mime类型

    As you already said, by using the following, you can make these execute in the PHP. 正如您已经说过的那样,通过使用以下内容,可以使它们在PHP中执行。

     application/x-httpd-php phtml pht php js html css 

    Or another way is this: 或另一种方式是这样的:

     <FilesMatch "^.*?css*?$"> SetHandler php5-script </FilesMatch> 

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

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