Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

1.3K
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda