Articles | Digital Delivery

Configure Apache Web Server for Angular Application With htaccess

1 min read
angular apache web deploy config

When deploying an Angular application on Apache web server, you may need to create rewrite rules to handle app paths. By default, Angular uses the HTML5 History API to manage routing, which means that the app's URLs do not contain a file extension or a hash symbol. This can cause issues when trying to access app routes directly, as Apache may not know how to handle them. In this post, we will discuss how to create a .htaccess file for an Angular app to make rewrite rules for app paths.

Create .htaccess file

The first step is to create a .htaccess file in the root directory of your Angular app. This file will contain the rewrite rules that tell Apache how to handle app paths. Open a text editor and create a new file called ".htaccess". Save it in the root directory of your Angular app.

Add rewrite rules

Once you have created the .htaccess file, you can add rewrite rules to it. The following rewrite rule will redirect all requests to the index.html file in your app's root directory:

RewriteEngine On RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L

This rule tells Apache to redirect all requests to the index.html file, except for requests for files or directories that exist on the server.

Test the rewrite rules

Save the .htaccess file and upload it to your server. You can now test the rewrite rules by accessing your Angular app in a web browser. Try accessing app routes directly by typing them into the address bar. If the rewrite rules are working correctly, you should be able to access app routes without any issues.