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!!
CodePudding user response:
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:
- Remove the slash prefix on the substitution string (
/index.html/
) - And remove the trailing slash. (Ordinarily this would result in a 404, unless path-info is enabled?)
- Remove the
RewriteBase
directive altogether. - 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]