Search code examples
reactjsapache.htaccessmod-rewrite

Refreshing page gives "404 page not found", deploy react build in subdirectory


I'm trying to upload react application with multi routes build file on DigitalOcean server in subdirectory (/admin). When we refresh page. it shows 404 page not found. Example URL:http://example.com/admin/home

Please check below .htaccess file commands

 <IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /admin/
  RewriteRule ^index\.html$ - [L]

  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l

  RewriteRule ./index.html [L]

</IfModule>

not families with react build upload.


Solution

  • If the .htaccess and index.html files are inside the /admin subdirectory then the .htaccess file should look like this:

    Options +FollowSymLinks
    DirectoryIndex index.html
    
    RewriteEngine On
    
    RewriteRule ^index\.html$ - [L]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule . index.html [L]
    

    Note there are 3 arguments in the last rule, space separated
    (ie. RewriteRule<space>.<space>index.html<space>[L]):

    • . - a single dot. The pattern. Matches everything except the document root.
    • index.html - the substitution string. No slash prefix.
    • [L] - flags. L indicates the last rule and stops the current round of processing.

    Note also that I've removed the RewriteBase directive altogether. And removed the slash prefix from the start of the substitution string. The <IfModule> wrapper should also be removed.

    RewriteRule ./index.html [L]
    

    The syntax of your original rule (above) was incorrect as you appear to have either missed the first argument entirely or combined both arguments into one.


    From comments...

    Try changing form RewriteRule ./index.html [L] To RewriteRule ^ admin/index.html [L]
    @RavinderSingh13

    Since the RewriteBase directive is still present (and the .htaccess file is inside the /admin directory anyway) this would have resulted in an erroneous rewrite to /admin/admin/index.html (a 404).

    RewriteRule ^admin/index.html [L]
    @manish hedau

    However, you had omitted the space after the first argument, so this would not have done anything (the rule would not have matched).

    I have put just only ------------ in .htacess file but i am getting not found message.
    i will check .htaccess is enable or not and let you know.

    Is .htaccess enabled? Do you have access to the server config?

    Bear in mind that if you are using LiteSpeed (not Apache) then typing ----- at the top of the file will be silently ignored - no error.