给定一个在构建上具有动态生成的产品URL的盖茨比应用程序:
- /x/x
- /x/
- product/x/variant1//x/x/x/variant2//
- y/y/y/
- y/y/y/variant1/
- ...
是否有办法自动导致对每种产品的不存在的子净值访问以重定向到该产品的根源?最好在服务器端( gatsby-ssr.js
)上。
这样:
- /product/x/variantDoesnotexist/=> /product/x
- //product/x/* => /product/x/( * *不是有效的URL零件)
- /product/y/variantDoesnotexist/=> /product/y/
- ...
到目前为止仅找到了客户端解决方案,其中包括检查404页模板中的检查路径和进行有条件的 window.location.location.assign
。这不是最佳的,因为它在重定向之前闪烁404页,并且不执行302/301重定向。
Given a Gatsby application with dynamically generated product urls on build:
- /product/x/
- /product/x/variant1/
- /product/x/variant2/
- /product/y/
- /product/y/variant1/
- ...
Is there a way to automatically cause access to non-existing sub-urls of each product to redirect to the root of that product? Preferably on the server-side (gatsby-ssr.js
).
Like this:
- /product/x/variantdoesnotexist/ => /product/x/
- /product/x/* => /product/x/ (When * is not a valid url part)
- /product/y/variantdoesnotexist/ => /product/y/
- ...
Only found a client-side solution so far, which includes checking path in the 404 page template and doing a conditional window.location.assign
. This is not very optimal as it flashes the 404 page before redirecting and does not perform a 302/301 redirect.
发布评论
评论(1)
您可以在
gatsby-node.js
中使用createredirect
操作:根据需要对其进行调整,您可以在
createredirect
中使用当前的重定向规则。请记住,在
createPages
中是创建所有动态页面的地方,因此您拥有所有可用的slug和路径来使您的动态重定向。更多详细信息:
You can use
createRedirect
action in thegatsby-node.js
:Tweak it as you wish, you can use your current redirects rules within
createRedirect
.Keep in mind that in
createPages
is where you create all dynamic pages hence you have all the slugs and paths available to make your dynamic redirects.More details: https://support.gatsbyjs.com/hc/en-us/articles/1500003051241-Working-with-Redirects-and-Rewrites