1 March 2025

Automatically generate the .htaccess file for you each time you export your Expo web app.

Modify package.json

Source code viewer
  1. "scripts": {
  2. "build": "expo export -p web && node post-build.js"
  3. }
Programming Language: JSON

Create post-build.js in the root of your project

This script will create the .htaccess file inside the dist/ folder.
Source code viewer
  1. const fs = require("fs");
  2. const path = require("path");
  3.  
  4. const htaccessContent = `
  5. RewriteEngine On
  6. RewriteBase /
  7. RewriteCond %{REQUEST_FILENAME} !-f
  8. RewriteCond %{REQUEST_FILENAME} !-d
  9. RewriteRule . /index.html [L]
  10. `;
  11.  
  12. fs.writeFileSync(path.join(__dirname, "dist", ".htaccess"), htaccessContent, "utf8");
  13. console.log(".htaccess added successfully!");
Programming Language: ECMAScript

Run the build

Source code viewer
  1. npm run export
Programming Language: Bash