where to add htaccess file in angular 4 cli project to add rewrite rules? where to add htaccess file in angular 4 cli project to add rewrite rules? angularjs angularjs

where to add htaccess file in angular 4 cli project to add rewrite rules?


there is answer in github issue

Put .htaccess in src folder (final path for is src/.htaccess)

Add it to angular-cli.json in the assets array (like favicon.ico).

Have something similar to this:
"assets": [ "assets", "assets/images/favicon.ico", ".htaccess" ],


You can also copy files to dist folder after build by defining your build commands in package.json on key scripts e.g.

"buildProd": "ng build --prod  && cp ./.htaccess ./dist/project-name/.htaccess",

copy file .htaccess to main projet folder (not src) and run build command by npm run buildProd to create dist.

This approach not introduce server files to angular build step (it do it "outside").


By having .htaccess outside the src, you'll most likely to get the following error,

An unhandled exception occurred: The .htaccess asset path must start with the project source root.

The corrected approach is this,

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

Make sure to put your .htaccess file under src.

enter image description here

With the above settings, when you run your build, you'll get the .htaccess inside the dist.

enter image description here