Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

1.3K
Views
Configuring .htaccess for React applications (+Router) inside a subdirectory

I have a React application which is located in the mysite.com/admin directory. So index.html is located in mysite.com/admin/index.html. The application contains routes like /admin/users or /admin/tools/removeUser.

How do I configure .htaccess to load mysite.com/admin/index.html when I open mysite.com/admin/ for exapmle? The url itself should remain the same in order to render necessary route.

Should say that there is also a React app in the root directory. Here is what I tried:

RewriteEngine on
RewriteBase /

#for app in subdirectory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^admin admin/index.html [L]

#for root app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . /index.html [L]

But when I open mysite.com/admin/tools/removeUser for example, in the console I get this error:

enter image description here

What am I doing wrong?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

You're right.

Potential problem.

When run on local server Node JS routing (components are BrowserRouter, Switch, Route ) is work fine. Opening direct link and reload page correct work. But when deploy on web server Apache are situation, start component app work (because page don’t reload), but when reload page then appear 404 page. The same page 404 appear when opening direct link contens a part of route app. Server Appache requires correct settings in file .httpaccess For example I have two CMS on server apache and one of them is SPA. Мy CMS should have rules and another system like SPA should have rules in .httpaccess

Possible solutions:


    RewriteEngine on
    #for app in subdirectory
    RewriteBase /admin/
    RewriteRule ^index\.html$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^.*admin/.* /admin/index.html [L]
    #for root app
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule . /index.html [L]

It is important that the rules for the application subdirectory do not overwrite the rules for the main application

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!