I'm new in React, and try to deploy app in my local machine using XAMPP. My problem is, when i tried to access the webpage, its gives 404 Not Found. When using npm start, its working properly.
I tried some advice from other similar question, but its still not working. This is my index.js
<React.StrictMode>
<BrowserRouter basename='/react'>
<Route exact path="/" component={Home} />
</BrowserRouter>
</React.StrictMode>
And I put "homepage": "http://localhost/react/" in package.json
"homepage": "http://localhost/react/",
"name": "admin-dashboard-presensi",
After that, I run npm run build and move the folder to htdocs/react
This is what I put inside .htaccess:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
Possible Error Sincaio:
1- You add url to package.json before you are build
2- missing to restart XAMPP server
3- make sure the folder build name is react, else rename directory or change base <BrowserRouter basename='/react'> and rebuild
4- also check the directory full path is correct, and its has permission which its allow you to visit it.
More Details: (Move the contents of the build folder into htdocs/react, not the build folder itself => Copied from @Phil)
If above points not work, you can check details bellow: I think you missing hosts and/or virtual host config:
<VirtualHost localhost:80>
ServerName my-website.local
ServerAlias www.my-website.local
ServerAdmin webmaster@my-website.local
DocumentRoot "path/to/your/build"
<Directory path/to/your/build>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order Deny,Allow
Allow from all
Require all granted
</Directory>
</VirtualHost>
Also you need to add your new domain to hosts:
vim /etc/hosts and add:
127.0.0.1 my-website.local
Also, you need to make sure you are put .htaccess in public directory before build:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
and then add: "homepage": "http://www.my-website.local", to package.json
Rebuild again, and restart XAMPP.
Note: I add alias domain nested of normal localhost, its better, but you can ignore that.