What is the best approach for redirection of old pages in Jekyll and GitHub Pages?

前端 未结 8 509
萌比男神i
萌比男神i 2020-12-07 09:38

I have blog on github pages - jekyll

What is the best way to solve url strategy migration?

I found the best practice in common is create htaccess like so

8条回答
  •  独厮守ぢ
    2020-12-07 09:47

    redirect-from plugin

    https://github.com/jekyll/jekyll-redirect-from#redirect-to

    It is supported by GitHub and makes it easy:

    _config.yml

    gems:
      - jekyll-redirect-from
    

    a.md

    ---
    permalink: /a
    redirect_to: 'http://example.com'
    ---
    

    as explained at: https://help.github.com/articles/redirects-on-github-pages/

    Now:

    firefox localhost:4000/a
    

    will redirect you to example.com.

    The plugin takes over whenever the redirect_to is defined by the page.

    Tested on GitHub pages v64.

    Note: this version has a serious recently fixed bug which wrongly reuses the default layout for the redirect: https://github.com/jekyll/jekyll-redirect-from/pull/106

    Manual layout method

    If you don't feel like using https://github.com/jekyll/jekyll-redirect-from it's easy to implement it yourself:

    a.md

    ---
    layout: 'redirect'
    permalink: /a
    redir_to: 'http://example.com'
    sitemap: false
    ---
    

    _layouts/redirect.html based on Redirect from an HTML page :

    
    
    
      
      Redirecting...
      {% comment %}
        Don't use 'redirect_to' to avoid conflict
        with the page redirection plugin: if that is defined
        it takes over.
      {% endcomment %}
      
      
    
    
      

    Redirecting...

    Click here if you are not redirected.

    Like this example, the redirect-from plugin does not generate 301s, only meta + JavaScript redirects.

    We can verify what is going on with:

    curl localhost:4000/a
    

提交回复
热议问题