Skip to content

Commit a6c2385

Browse files
Copilotmnaimfaizy
andcommitted
Fix SPA routing on cPanel/Apache: add public/.htaccess + deployment docs
Co-authored-by: mnaimfaizy <34773846+mnaimfaizy@users.noreply.github.com>
1 parent e298116 commit a6c2385

2 files changed

Lines changed: 65 additions & 0 deletions

File tree

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,51 @@ yarn build
172172

173173
The built application will be in the `dist/` directory.
174174

175+
### 🌐 Deploying to cPanel (Apache)
176+
177+
Code Executives is a **Single Page Application (SPA)** that uses client-side routing via React Router. When deployed on a traditional Apache/cPanel host, you need to tell Apache to serve `index.html` for every URL so that React Router can handle navigation. Without this step, directly visiting any URL (e.g. `https://codexecutives.com/javascript`) will return a **404 error** because Apache looks for a physical file at that path.
178+
179+
**This repository already includes the required `.htaccess` file** (`public/.htaccess`) which Vite automatically copies to `dist/` on every build.
180+
181+
#### Step-by-step cPanel deployment
182+
183+
1. **Build the application**
184+
185+
```bash
186+
npm run build
187+
```
188+
189+
The `dist/` folder now contains the complete production bundle, including the `.htaccess` file.
190+
191+
2. **Upload `dist/` contents to your public_html directory**
192+
193+
Using cPanel's File Manager or FTP, upload **all files and folders** inside `dist/` (not the folder itself) to your domain's `public_html` directory (or the sub-directory you are deploying to).
194+
195+
> ⚠️ Make sure to upload hidden files as well — the `.htaccess` file starts with a dot and can be invisible in some FTP clients. Enable "Show Hidden Files" in cPanel File Manager.
196+
197+
3. **Verify `mod_rewrite` is enabled**
198+
199+
The `.htaccess` relies on Apache's `mod_rewrite` module. Most cPanel hosts have this enabled by default. If you still see 404 errors after uploading, contact your host to confirm `mod_rewrite` is active.
200+
201+
4. **Test direct URL access**
202+
203+
Open your browser and navigate directly to a deep URL such as `https://yourdomain.com/javascript` or `https://yourdomain.com/git`. The page should load correctly via React Router.
204+
205+
#### How the `.htaccess` works
206+
207+
```apache
208+
Options -MultiViews
209+
RewriteEngine On
210+
RewriteCond %{REQUEST_FILENAME} !-f
211+
RewriteCond %{REQUEST_FILENAME} !-d
212+
RewriteRule ^ index.html [QSA,L]
213+
```
214+
215+
- Requests for **real files** (JS/CSS chunks, images, fonts) are served directly.
216+
- All other requests are **internally rewritten** to `index.html`, letting React Router decide what to render.
217+
218+
> **No Node.js server is required.** The application is a fully static bundle that works with any web server capable of serving static files.
219+
175220
## 🎓 Learning Modules
176221

177222
### 📚 **Git Tutorial (Complete)**

public/.htaccess

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# ============================================================
2+
# Apache configuration for cPanel/Apache SPA (Single Page App)
3+
# ============================================================
4+
# This file is required so that React Router can handle client-
5+
# side routing when a user directly visits any URL path (e.g.
6+
# https://codexecutives.com/javascript). Without it, Apache
7+
# would return a 404 for any path that does not map to a real
8+
# file on disk.
9+
# ============================================================
10+
11+
Options -MultiViews
12+
RewriteEngine On
13+
14+
# If the request is for an existing file or directory, serve it
15+
# directly (assets, images, JS/CSS chunks, etc.).
16+
RewriteCond %{REQUEST_FILENAME} !-f
17+
RewriteCond %{REQUEST_FILENAME} !-d
18+
19+
# Otherwise redirect to index.html so React Router takes over.
20+
RewriteRule ^ index.html [QSA,L]

0 commit comments

Comments
 (0)