React Deployment on Separate Domain
This guide explains how to host frontend-source (React frontend) on a different domain
than your Laravel backend API.
Frontend:
https://app.yourdomain.comBackend API:
https://api.yourdomain.com/api/v1
Step 1: Build React App
Open terminal inside the frontend-source folder and run:
npm install
npm run build
This generates production files in frontend-source/dist.
Step 2: Configure Environment
In the current release package, the frontend API URL is configured through Vite environment
variables. Create a .env file inside frontend-source based on
.env.example.
VITE_API_URL=https://api.yourdomain.com/api/v1
VITE_API_BASE_URL=https://api.yourdomain.com
VITE_API_URL is the main API endpoint used by the frontend. Keep the
/api/v1 suffix. VITE_API_BASE_URL is used for backend-origin media and CMS
URLs and should normally be your backend domain without /api/v1.
After updating the environment file, run build again:
npm run build
Step 3: Upload Build Files
- Create/subdomain for frontend (for example
app.yourdomain.com). - Upload all files from
frontend-source/distto that domain document root. - Do not upload source files; only the built
distoutput.
Step 4: Configure SPA Rewrite
React Router needs fallback to index.html.
Apache (.htaccess)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
Nginx
server {
listen 80;
server_name app.yourdomain.com;
root /var/www/frontend-source/dist;
index index.html;
location / {
try_files $uri $uri/ /index.html;
}
}
Step 5: Backend CORS
Allow frontend domain in backend CORS config: config/cors.php.
For production, use explicit domain list instead of wildcard.
'allowed_origins' => [
'https://app.yourdomain.com',
],
Step 6: SSL (Required)
- Enable HTTPS on both frontend and backend domains.
- Do not mix HTTP frontend with HTTPS backend (or vice versa).
- Renew certificates and force HTTPS redirects.
Step 7: Production Checklist
- Frontend domain opens successfully.
- Login request reaches backend API correctly.
- No browser CORS errors in Developer Console.
- Refresh on deep routes (example:
/dashboard) works. - Payment callback/return URLs are reachable publicly.
cPanel Quick Steps
- Create subdomain (example:
app.yourdomain.com). - Upload
distfiles into subdomain root. - Create
.htaccesswith SPA rewrite rules. - Enable SSL certificate.
- Test login and key routes.
Common Problems and Fix
- Blank page: Wrong build upload path. Upload the contents of
dist, not the folder itself. - 404 on refresh: Missing SPA rewrite config.
- Network/CORS blocked: Add frontend domain in backend
allowed_origins. - Still calling old or wrong API: Recheck
.env, confirmVITE_API_URL, then rebuild the frontend.