Angular .htaccess for root and subdirectory

Viewed 758

I am using multiple angular projects on single server, This is my folder structure of my projects

Root: Project-1
Root/test : Project2
Root/test/new : project3

Here is the htaccess code, that I am using. but subdirectory projects not working some times

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

RewriteRule ^(.*) /index.html [NC,L]

The issue is, when I am trying to access the second project using this url, www.domain.com/test, its redirecting to root folder, that is project1 . I think it's checking project1 routing, when iIaccess project2

How can I fix the issue.

2 Answers

I have a similar project structure and the below .htaccess works for me.

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

See if it works.

Look my friend. What you want to do is easily solved.

There are 2 important places to make your app start url clear to Angular:

1 - In the index.html file

2 - In the .htaccess file on the server

The changes that you must make for the "Project2" are the following.

1 - You look within your code in index.html of your Angular app for the following line of code:

<base href = "/" />

You will modify it by putting the following

<base href = "/test/" />

2 - You create the .htacces file in the root of the Root/test directory with the following content:

RewriteEngine On

RewriteCond% {HTTPS}! = On
RewriteRule ^ (. *) $ Https: //% {HTTP_HOST}% {REQUEST_URI} [L, R = 301, NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"


# If a path or directory request exists, go to it as is

RewriteCond% {DOCUMENT_ROOT}% {REQUEST_URI} -f [OR]
RewriteCond% {DOCUMENT_ROOT}% {REQUEST_URI} -d
RewriteRule ^ - [L]

# If the source of the request does not exist, use index.html
RewriteRule ^ /index.html

Note that in the last line RewriteRule ^ /index.html and change it to your path to the index.html file of your compilation, leaving it as follows:

RewriteRule ^ /test/index.html

That is all. Now you will do the same procedure for Project3 and you will see how everything works perfectly. Ej: In index.html <base href = "/test/new/" /> and in .htaccess RewriteRule ^ /test/new/index.html

Related