我在/pages目录中添加了一个自定义的404.js文件,它在本地运行良好,但是当我运行下一个导出并将所有文件从out目录放到cpanel中的public_html目录时,一切都正常,但是当我输入错误的路径时,它应该转到404页,而我的web会带我到主页,在地址栏中有相同的链接。例如:当我尝试访问/about页面时,我没有/about页面,我的网站应该向我展示404页面,而不是显示带有/about的主页(index.html)。
next.config.js文件
module.exports = {
exportPathMap: async function (
defaultPathMap,
{ dev, dir, outDir, distDir, buildId }
) {
return {
"/": { page: "/" },
"/faq": { page: "/faq" },
"/how-it-works": { page: "/how-it-works" },
"/patient-onboarding": { page: "/patient-onboarding" },
"/privacy-policy": { page: "/privacy-policy" },
"/terms-of-use": { page: "/terms-of-use" },
"/searched-pharmacy": { page: "/searched-pharmacy" },
"/new-customer": { page: "/new-customer" },
"/404": { page: "/404" }
}
},
trailingSlash: true,
}
使用"next":"^10.0.6",
server.js文件
const express = require("express");
const next = require("next");
const dev = process.env.NODE_ENV !== "production";
const sitemapOptions = {
root: __dirname + "/public/",
headers: {
"Content-Type": "text/xml;charset=UTF-8",
},
};
// Create the Express-Next App
const app = next({ dev });
const handle = app.getRequestHandler();
//Start the app
app
.prepare()
//Start Express server and serve the
.then(() => {
const server = express();
// serve sitemap
server.get("/sitemap.xml", (req, res) =>
res.status(200).sendFile("sitemap.xml", sitemapOptions)
);
server.get("*", (req, res) => {
return handle(req, res);
});
server.listen(3000, (err) => {
if (err) throw err;
console.log("> Ready on http://localhost:3000");
});
})
.catch((ex) => {
console.error(ex.stack);
process.exit(1);
});
发布于 2021-02-22 07:18:46
终于来了!我找到了解决问题的办法。多亏了本文件
在我的NextJS配置中没有问题,问题是我的.htaccess文件。我已经更改了.htaccess文件,404页现在正在使用HTTP状态代码404。
我刚刚完成了这两个简单的步骤!
ErrorDocument 404 /404.html
中添加了这一行注意:-- .htaccess和404.html文件都位于root_level
https://stackoverflow.com/questions/66266279
复制相似问题