next-htaccess-generator
v1.0.4
Published
This is a simple package to generate htaccess file from next build.
Maintainers
Readme
Node .htaccess Generator
This project simplifies the process of creating an .htaccess file for Apache web servers using Node.js. It is specifically designed for Next.js projects but can be adapted for other static site generators as well. The .htaccess file generated by this tool enables clean and SEO-friendly URLs for your static website.
Installation
To install the package as a development dependency, run the following command in your terminal:
npm install next-htaccess-generator --save-devConfiguration
Before using the generator, you need to configure it by creating a jay.config.json file at the root of your project folder. This file should contain the following properties:
{
"buildPath": "./out",
"include404": false,
"outputPath": "out"
}buildPath: Specifies the directory where your Next.js static build is located (e.g.,./out).include404: A boolean value indicating whether to include rewrite rules for 404 pages. Set it tofalseif you want to exclude 404 pages.outputPath: Defines the path where the generated.htaccessfile should be saved.
Usage
After configuring the jay.config.json file, you can add a script to your package.json to generate the .htaccess file:
"scripts": {
"build-htaccess": "next-htaccess-generator build"
}To generate the .htaccess file, run the following command:
npm run build-htaccessThis command scans the directory specified in buildPath for HTML files, generates URL rewrite rules for each file, and saves them to the outputPath directory.
How it Works
The generator script follows these steps:
- Reads the configuration from the
jay.config.jsonfile. - Recursively scans the specified
buildPathdirectory and collects all HTML file paths. - Generates Apache URL rewrite rules for each HTML file.
- Optionally adds a catch-all rule to redirect unmatched URLs (unless
include404is set totrue). - Writes the generated rules to an
.htaccessfile in the specifiedoutputPathdirectory.
The resulting .htaccess file can be placed in the root directory of your Apache web server, enabling clean and SEO-friendly URLs for your Next.js static build.
