Skip to content
Browser messages3 min read

404 when refreshing or opening an inner page in a React or Vue app

404 Not Found (only when refreshing an inner page)

Short answer

In a single-page app (React, Vue, Vite) there is no real file called /about on the server; routing happens in the browser. When you refresh, the browser asks the server for /about and gets a 404. The fix is an .htaccess file next to index.html that sends every URL that is not a real file or folder to index.html, so the app can show the right page.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. Why it happens
  2. The fix: a rewrite rule in .htaccess
  3. Look-alike problems
  4. How to confirm it is fixed
  5. On ArabyHost

The symptom is easy to spot: the home page works, clicking around works, but refreshing a URL such as example.com/products/12, or opening it from WhatsApp or Google, shows the server’s own 404 Not Found page. The app is fine. The server is missing one instruction.

Why it happens

A single-page app (SPA) has exactly one HTML file, index.html. The router library (React Router, Vue Router) reads the URL in the browser and renders the right component. The server knows nothing about those routes: when it receives /products/12 it looks for a file or folder with that name inside public_html and finds none.

The fix: a rewrite rule in .htaccess

1. Create the file in the right place

Open File Manager and go to the folder that holds index.html (usually public_html). Under Settings, enable Show Hidden Files, then create a file named .htaccess or edit the existing one:

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

What it says: a request for a file that really exists (an image, a JS file) or an existing folder is served as is; any other URL goes to index.html, and the app reads the URL and shows the right page. This is the rule the Vue Router documentation gives for Apache, and LiteSpeed reads it the same way. The file itself is explained in what is .htaccess.

2. If the app lives in a subfolder

For an app in public_html/app, put .htaccess inside app and change two lines:

  RewriteBase /app/
  RewriteRule . /app/index.html [L]

This must match base in vite.config.js and basename in React Router, or you get a blank page.

3. Make the file part of the build

Every time you upload a fresh dist you can wipe .htaccess without noticing. Put the file in the public folder of your Vite or Create React App project, and it is copied into the build output on every npm run build.

4. If the app is served by Node.js

If the frontend is served by an Express app through Setup Node.js App, Express does not read .htaccess. Add a fallback at the end of your code, after the API routes and static files:

app.use(express.static(path.join(__dirname, 'dist')));
app.use((req, res) => {
  res.sendFile(path.join(__dirname, 'dist', 'index.html'));
});

This form works in both Express 4 and 5, unlike app.get('*'), whose behaviour changed in Express 5.

Look-alike problems

  • CSS or JS files come back as HTML: with the rule in place but wrong asset paths, the rule returns index.html instead of the missing file and you get a MIME type error. See CSS and JS not loading.
  • Laravel: if the frontend lives inside Laravel, routing is Laravel’s job; see Laravel routes return 404.
  • Letter case: /About and /about are different routes if you defined them in lowercase.

How to confirm it is fixed

Open an inner URL in a private window and reload. You should get the same page, not a 404. Try a URL that does not exist, like /xyz: your app’s own “not found” view should appear if it has one, which is expected.

On ArabyHost

LiteSpeed reads .htaccess and mod_rewrite rules the way Apache does, so the rule above works on every plan without asking support. You create and edit the file in File Manager from the panel, and if the error persists the Araby AI assistant reads the error log and explains why. Node.js apps that serve their own frontend run on the AI/Developer plans.

FAQ

Why do links work when clicked but fail on refresh?

Clicking a link inside the app lets React Router change the address without asking the server for anything. A refresh, or opening the link from a message, sends a real request to the server, which has no file by that name.

I uploaded .htaccess and nothing changed. Why?

Check that it sits in the same folder as index.html and that it is named with a leading dot and no .txt extension. Turn on Show Hidden Files in File Manager to see it, since dotfiles are hidden by default.

Is HashRouter a good fix?

It works with no server setup because URLs become /#/about, but they look worse and are weaker for search engines. The .htaccess rule is the better fix on LiteSpeed or Apache hosting.

Does Next.js need the same rule?

If you export it as static files with output: 'export', every page becomes its own HTML file and you may need trailingSlash: true. If you run it as a Node.js server, the server handles routes and you do not need this rule.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.