Skip to main content

NextJs htaccess setting

The content of the .htaccess for nextjs application is

# Redirect traffic to your port 3001
DirectoryIndex
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)?$ http://127.0.0.1:3001/$1 [P,L]
 

to exclude a file from this rule (EX: test.txt) we can add this like 

RewriteCond %{REQUEST_URI} !test\.txt [NC]

 

Full Code

# Redirect traffic to your port 3001
DirectoryIndex
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !test\.txt [NC]
RewriteRule ^(.*)?$ http://127.0.0.1:3001/$1 [P,L]

Redirect URL that contains media to another URL

RewriteRule ^media/(.*)$ https://example.com/media/$1 [R=302,NC,L]

 

full code 

# Redirect traffic to your port 3001
DirectoryIndex
RewriteEngine On
RewriteBase /
RewriteRule ^media/(.*)$ https://example.com/media/$1 [R=302,NC,L]
RewriteRule ^(.*)?$ http://127.0.0.1:3001/$1 [P,L]

redirect PayPal posts to another URL, this usually very important if you have storefront linked to another backend URL, EX: /paypal/ipn/

RewriteRule ^paypal/(.*)$ {backend_url}/paypal/$1 [R=302,NC,L]

Full Code

# Redirect traffic to your port 3001
DirectoryIndex
RewriteEngine On
RewriteBase /
RewriteRule ^media/(.*)$ https://example.com/media/$1 [R=302,NC,L]

RewriteRule ^paypal/(.*)$ https://example.com/paypal/$1 [R=302,NC,L]
RewriteRule ^(.*)?$ http://127.0.0.1:3001/$1 [P,L]

 

Note:

Redirect sitemap.xml to the backend domain

RewriteRule ^sitemap\.xml$ https://www.backend.com/sitemap.xml [R=301,L]