apache 如果可用,如何配置 mod_rewrite 以提供缩小的文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/243124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:54:50  来源:igfitidea点击:

How to configure mod_rewrite to serve minified files, if available?

apachemod-rewriteminify

提问by Stepan Stolyarov

Here is the problem: we have lots of Javascripts and lots of CSS files, which we'd rather be serving minified. Minification is easy: set up the YUI Compressor, run an Ant task, and it spits out minified files, which we save beside the originals.

问题是:我们有很多 Javascript 和很多 CSS 文件,我们宁愿将它们缩小。缩小很容易:设置 YUI 压缩器,运行 Ant 任务,它会输出缩小的文件,我们将这些文件保存在原始文件旁边。

So we end up with the following directory structure somewhere inside our DocumentRoot:

所以我们最终在我们的 DocumentRoot 中的某处得到以下目录结构:

/
   /js
      /min
         foo-min.js
         bar-min.js
      foo.js
      bar.js
      quux.js
   /css
      ...

Now what we need is that Apache serve files from the minsubdirectory, and fallback to serving uncompressed files, if their minified versions are not available. The last issue is the one I cannot solve.

现在我们需要的是 Apache 从min子目录中提供文件,如果它们的缩小版本不可用,则回退到提供未压缩的文件。最后一个问题是我无法解决的。

For example: suppose we have a request to example.com/js/foo.js— in this case Apache should send contents of /js/min/foo-min.js. There is no minified quux.js, so request to /js/quux.jsreturns /js/quux.jsitself, not 404. Finally, if there is no /js/fred.js, it should end up with 404.

例如:假设我们有一个对example.com/js/foo.js的请求——在这种情况下,Apache 应该发送/js/min/foo-min.js 的内容。没有缩小的quux.js,所以对/js/quux.js 的请求返回/js/quux.js本身,而不是 404。最后,如果没有/js/fred.js,它应该以 404 结束。

Actually, I'm setting build scripts in such a way that unminified files are not deployed on the production server, but this configuration still might be useful on an integration server and on development machines.

实际上,我设置构建脚本的方式是未在生产服务器上部署未缩小的文件,但此配置在集成服务器和开发机器上仍然可能有用。

回答by Stepan Stolyarov

Here is the configuration that finally worked:

这是最终有效的配置:

/js/.htaccess:

/js/.htaccess:

RewriteEngine On

RewriteBase /js

RewriteCond %{REQUEST_URI} ^/js/((.+)\.js)$
RewriteCond %{DOCUMENT_ROOT}/js/min/%2-min.js -f
RewriteRule ^(.+)$ min/%2-min.js [L]

Same for cssdirectory.

同为CSS目录。

回答by Ignacio Vazquez-Abrams

You can use RewriteCond to detect the presence of a minified file:

您可以使用 RewriteCond 来检测缩小文件的存在:

RewriteCond %{REQUESTURI} ^/js/(.*\.js)
RewriteCond js/min/%1 -f
RewriteRule %1 min/%1 [L]

回答by Hank Gay

Is it possible to change your build scripts? If so, you can configure them to minify the files and give them the same file name, but only when provided the proper flag, e.g. ant productionDeployinstead of ant integrationDeploy. That way the minification process is completely transparent to everything except the build script.

是否可以更改您的构建脚本?如果是这样,您可以配置它们以缩小文件并为它们提供相同的文件名,但前提是提供了正确的标志,例如ant productionDeploy代替ant integrationDeploy. 这样,除了构建脚本之外,缩小过程对所有内容都是完全透明的。