Search code examples
node.jsreactjs.htaccessnext.js

Next.JS - `.htaccess` file?


I'm just starting out with my first Next.JS app. I've used npx create-next-app and have made a few pages, when I realized that I'm not sure how to use a .htaccess file. I'm used to Apache taking care of this stuff for me, and simply putting my .htaccess file into my Next.JS app's root directory unsurprisingly didn't seem to cut it. How would I go about setting up a .htaccess file similar to the following?

RewriteEngine on

RewriteRule ^profile/([a-z0-9]+) profile.html

Solution

  • .htaccess files are specific to Apache, so without Apache you can't use them. You can use Apache as a proxy to your node.js app, but you would still not use a .htaccess file; you could configure RewriteRules in your Apache config but there is no need to when you can handle all your routing directly in your application logic.

    In node.js you don't need a separate web server like Apache. Your program can be long-running, bind to a port, and listen and respond to requests which is the main functionality that a web-server normally provides.

    Next.JS has documentation for setting up custom routing here: https://nextjs.org/docs/#custom-server-and-routing