1

I have an issue with redirecting in my Next.js app.

I did static export (next build && next export).

Host of the app is located in the subdirectory (https://www.myhost.com/subdirectory/**[index.html here]**) so I used basePath: '/subdirectory' in next.config.js. Everything works just right on first sight.

Redirecting inside the app works just fine. But when user refresh the page on the other page then index - let's say the page is named subpage (https://www.myhost.com/subdirectory/subpage) the site doesn't redirect the user to subpage but rather goes to https://www.myhost.com/ (not even to the subdirectory in which the project is located).

I would very much appreciate someones help.

Here is .htaccess file.

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html/ [L]

</IfModule>

If u need any other file, I can edit this post if u comment it.

Thanks in advance!!

0

1 Answer 1

1

I'm assuming your .htaccess file is located at /subdirectory/.htaccess. These directives are written as if the app is in the document root, not the subdirectory.

<IfModule mod_rewrite.c>

  RewriteEngine On
  RewriteBase /
  RewriteRule ^index\.html$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME} !-l
  RewriteRule . /index.html/ [L]

</IfModule>

The last RewriteRule directive sends all requests to the document root. You need to:

  1. Remove the slash prefix on the substitution string (/index.html/)
  2. And remove the trailing slash. (Ordinarily this would result in a 404, unless path-info is enabled?)
  3. Remove the RewriteBase directive altogether.
  4. The <IfModule> wrapper is not required.

Taking the above points into consideration, try the following instead:

# /subdirectory/.htaccess

RewriteEngine On

RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule . index.html [L]
1
  • 1
    U are a life saver! Thanks! It works now :) And yes, .htacces was a typo, I edited the question now :) Commented Oct 12, 2021 at 20:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.