[英]How to redirect pages/app folder to subdomain in next.js
I search a lot for this on the internet but I don't find any article related to it.我在互联网上搜索了很多,但我没有找到任何与之相关的文章。
Like I have a folder called pages in the root of my project and below tree is files of it.就像我在项目的根目录中有一个名为 pages 的文件夹,树下面是它的文件。
| 404.js
| auth.js
| index.js
| _app.js
| _error.js
\---app
index.js
next.js gives default behavior when someone opens project.local:3000
it will open index.js
and project.local:3000/app
it will open app/index.js
but I want that when someone open app.project.local:3000
it will open app/index.js
. next.js 给出默认行为,当有人打开
project.local:3000
它将打开index.js
和project.local:3000/app
它将打开app/index.js
但我希望当有人打开app.project.local:3000
它会打开app/index.js
。
My Hosts file我的主机文件
127.0.0.1 project.local
127.0.0.1 app.project.local
In short简而言之
I want to redirect pages/app
folder to app.project.local
or app.example.com
in next.js我想将
pages/app
文件夹重定向到 next.js 中的app.project.local
或app.example.com
I'm still a noobie in next.js (2nd day learning), but I was searching for subdomain support and I found three solutions on this Github issue: https://github.com/vercel/next.js/issues/5682我仍然是 next.js(第 2 天学习)中的菜鸟,但我正在寻找子域支持,我在这个 Github 问题上找到了三个解决方案: Z5E056C500A1C4B6A7110B50D807BADE.jsZissue://github.com/50D807BADE.5
server.js
file server.js
文件中实现的Most updated solution最新的解决方案
I found the solution while exploring the documentation on redirects .我在探索有关重定向的文档时找到了解决方案。
In your Next.js's root folder, create a vercel.json
file and then insert your redirects as object inside redirects
array like so:在 Next.js 的根文件夹中,创建一个
vercel.json
文件,然后将重定向作为 object 插入到redirects
数组中,如下所示:
{
"redirects": [
{ "source": "/blog", "destination": "https://blog.example.com" }
]
}
This will only work on production environment.这仅适用于生产环境。 It should work as intended.
它应该按预期工作。
NextJS now supports Locales: https://nextjs.org/docs/advanced-features/i18n-routing . NextJS 现在支持语言环境: https://nextjs.org/docs/advanced-features/i18n-routing 。
You can specify a locale in your config, eg admin
and specify the URL like this:您可以在配置中指定区域设置,例如
admin
并指定 URL,如下所示:
// next.config.js
module.exports = {
i18n: {
// These are all the locales you want to support in
// your application
locales: ['en-US', 'admin', 'nl-NL'],
// This is the default locale you want to be used when visiting
// a non-locale prefixed path e.g. `/hello`
defaultLocale: 'en-US',
// This is a list of locale domains and the default locale they
// should handle (these are only required when setting up domain routing)
// Note: subdomains must be included in the domain value to be matched e.g. "fr.example.com".
domains: [
{
domain: 'example.com',
defaultLocale: 'en-US',
},
{
domain: 'example.nl',
defaultLocale: 'nl-NL',
},
{
domain: 'admin.example.com',
defaultLocale: 'admin',
// an optional http field can also be used to test
// locale domains locally with http instead of https
http: true,
},
],
},
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.