Angular:如何为每个配置添加唯一的 .htaccess 文件? [英] Angular: how to add a unique .htaccess file per configuration?

查看:29
本文介绍了Angular:如何为每个配置添加唯一的 .htaccess 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Angular 应用包含 2 个构建配置:暂存和生产.

My Angular app contains 2 build configurations: staging, and production.

目前我有一个用于所有构建的标准 .htaccess 文件,将其包含在我的资产中:

Currently I have a standard .htaccess file for all builds, by including it in my assets:

"assets": [
  "src/.htaccess",
  "src/assets",
  "src/favicon.png"
],

<小时>

我想用 .htpasswd 文件密码保护我的临时构建目录,所以我更新了我的资产以包含它:


I want to password protect my staging build directory with a .htpasswd file, so I updated my assets to include it:

"assets": [
  "src/.htaccess",
  "src/.htpasswd"
  "src/assets",
  "src/favicon.png"
],

htpasswd 文件可以包含在每个构建中,因为没有在 .htaccess 文件中提及,它什么都不做.

The htpasswd file can be included in every build, because without a mention in the .htaccess file, it does nothing.

然后,我创建了第二个 .htaccess 文件,名为 .htaccess.dev,添加了以下几行:

Then, I've created a second .htaccess file, called .htaccess.dev, with the following added lines:

AuthName "Protected"
AuthUserFile /home/admin/domains/dev.***.com/public_html/.htpasswd
AuthGroupFile /dev/null
AuthType Basic
require valid-user

这个带有额外行的文件应该替换临时构建中的 .htaccess 文件,所以我在我的 angular.json 文件中添加了以下配置:

This file with the extra lines should replace the .htaccess file in the staging build, so I added the following configuration to my angular.json file:

"build": {
  ...
  "configurations": {
    "staging": {
      ...
      "fileReplacements": [
        {
          "replace": "src/.htaccess",
          "with": "src/.htaccess.dev"
        },
        {
          "replace": "src/environments/environment.ts",
          "with": "src/environments/environment.staging.ts"
        }
      ]
    }
  }
}

fileReplacements 适用于我的 .env,但 它不会替换我的 htaccess 文件.为什么?

fileReplacements works fine for my .env, but It does not replace my htaccess file. Why?

推荐答案

啊,看来 fileReplacements 只对 .ts 文件有效.

Ah, it seems that fileReplacements only works for .ts files.

angular/devkit#885:

目前,文件替换功能仅适用于 TS 文件.更具体地说 - 它仅适用于属于 webpack 编译器主机的文件.

Currently, the file replacement functionality works only for TS files. More specifically - it works only for files that are part of the webpack compiler host.

这将(显然)在 Angular 6.1 中得到修复.

This will (apparently) be fixed in Angular 6.1.

查看此 GitHub 线程了解更多信息

现在支持 (确认) 在 Angular 6.1 中

This is now supported (confirmed) in Angular 6.1

这篇关于Angular:如何为每个配置添加唯一的 .htaccess 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆