Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 216
APixel Visuals Avatar asked Jan 27 '23 16:01

APixel Visuals


1 Answers

If You use next export to SSG on your project This .htaccess file will fix redirection problem

RewriteEngine On
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
like image 152
Vino Crazy Avatar answered Jan 29 '23 07:01

Vino Crazy