apache 如何:为每个应用程序将图像、css 和 js 的请求重写到不同的文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1056287/
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 17:48:40  来源:igfitidea点击:

HOWTO: rewrite requests for images, css and js to different folders for each application?

apachemod-rewritecodeigniter

提问by picardo

I am trying to build a multi-app CodeIgniter site, where the assets for all apps will be stored in a single folder called “assets.” Inside this single folder, each app would have its own asset folder. So, if this is my root directory…

我正在尝试构建一个多应用程序 CodeIgniter 站点,其中所有应用程序的资产将存储在一个名为“资产”的文件夹中。在这个文件夹中,每个应用程序都有自己的资产文件夹。所以,如果这是我的根目录……

 user_guide
 apps_folder (this is where all my application folders live)
 system
 assets

...the assets folder will be organized thus:

...资产文件夹将这样组织:

assets
  myapp1
      js
      img
      css
      media

Here is my challenge. I am trying to write an apache directive that would rewrite all requests for files ending in css, js, png and gif to the respective asset directory for that app. But the user cannot know the exact location of those assets in the server. To give you an example:

这是我的挑战。我正在尝试编写一个 apache 指令,它将对以 css、js、png 和 gif 结尾的文件的所有请求重写到该应用程序的相应资产目录。但是用户无法知道这些资产在服务器中的确切位置。给你一个例子:

Here is the request: www.myapp1.com/js/jquery.js

这是请求:www.myapp1.com/js/jquery.js

The file is located in: assets/myapp1/js

该文件位于:assets/myapp1/js

The directive will check for a) the domain address b) file extension

该指令将检查 a) 域地址 b) 文件扩展名

...and based on those variables, will rewrite the url, thereby hiding thee real location of the asset, and make it possible to reference assets as if they were all located at the root of the website. Has anyone ever done something like this?

...并基于这些变量,将重写 url,从而隐藏资产的真实位置,并使引用资产成为可能,就好像它们都位于网站的根目录一样。有没有人做过这样的事情?

thanks,

谢谢,

采纳答案by the.jxc

This is totally possible. See the Apache rewrite guide:

这是完全可能的。请参阅 Apache 重写指南:

http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

http://httpd.apache.org/docs/1.3/misc/rewriteguide.html

Your rule will look something like this:

您的规则将如下所示:

RewriteCond   %{HTTP_HOST}  \.myapp1\.com$
RewriteRule  ^/js/(.*)\.js$ assets/myapp1/js/

Have fun!

玩得开心!

回答by SpliFF

You can use AliasMatch

您可以使用 AliasMatch

<VirtualHost myapp1>
   AliasMatch ^(.*\.)(js|css)$ /path/to/assets/myapp1
</VirtualHost>