gatsby - Gatsby netlify 重定向问题
问题描述
我正在为我的网站使用 gatsby 生成的静态站点并使用 Netlify 来托管它。我想防止从非尾随到斜杠的重定向。我们实施的一种解决方案是使用gatsby 插件来强制使用非尾随斜杠并禁用漂亮的 URL。但这会导致带有和不带有斜杠的页面返回 200。我想让我的静态站点将 301 从尾部重定向到非尾部斜杠页面。
解决方案
简短的回答:您不能使用这种方法直接从斜杠重定向到非斜杠。来自Netlify 的文档:
您不能使用重定向规则来添加或删除尾部斜杠。
可能对您有用的是使用.htaccess
方法,但我不确定它在 Netlify 的环境中会如何表现。像这样的东西应该工作:
#### Force HTTPS://WWW and remove trailing / from files ####
## Turn on rewrite engine
RewriteEngine on
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ https://www.example.com/$1/ [R=301,L]
# Force HTTPS and WWW
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
要.htaccess
在 Gatsby 中创建,您可以简单地在静态文件夹中创建它,并且在构建过程中它将被转译/克隆到/public
文件夹中,或者您可以使用gatsby-plugin-htaccess
插件。
推荐阅读
- z3 - 检测图中不存在循环:Z3 的 SMTLIB 格式
- php - 一个单词中的字符串拆分(PHP)
- stdout - 在 UART 中禁用 stdout 输出
- javascript - 来自多个搜索项的 Vue.js 粗体字词
- javascript - 在 Bigcommerce 中创建 JS 对象
- ios - 像 Facebook 一样向下滚动后如何从顶部滚动 - Swift 4.2
- wix - WIX - 如何将从注册表读取的文件位置传递给自定义操作?
- sapui5 - SAPUI5 应用程序中的 JQuery 日期滑块
- jquery - 隐藏除当前弹出窗口之外的所有弹出窗口?
- php - 从程序将数据保存或存储在数据库中