【问题标题】:Gatsby netlify redirection issueGatsby netlify 重定向问题
【发布时间】:2021-10-03 20:44:48
【问题描述】:

我正在为我的网站使用 gatsby 生成的静态站点并使用 Netlify 来托管它。我想防止重定向从非尾随斜杠。我们实施的一种解决方案是使用gatsby plugin 来强制使用非尾随斜杠并禁用漂亮的 URL。但这会导致带有和不带有斜杠的页面返回 200。我想让我的静态站点将 301 从尾部重定向到非尾部斜杠页面。

【问题讨论】:

    标签: gatsby netlify


    【解决方案1】:

    简短回答:您不能使用这种方法直接从尾部斜杠重定向到非尾部斜杠。来自Netlify's docs

    您不能使用重定向规则来添加或删除尾部斜杠。

    可能对您有用的是使用.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]
    

    要在 Gatsby 中创建 .htaccess,您可以简单地在静态文件夹中创建它,在构建过程中它将被转译/克隆到 /public 文件夹中,或者您可以使用 gatsby-plugin-htaccess 插件。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-02
      • 1970-01-01
      • 2021-08-11
      • 2020-06-01
      • 2021-05-29
      • 2023-03-15
      相关资源
      最近更新 更多