301 redirect for site hosted at github?

前端 未结 7 1219
没有蜡笔的小新
没有蜡笔的小新 2020-12-12 23:34

Here\'s a Github repository of mine: https://github.com/n1k0/casperjs

There\'s a gh-pages branch to hold the project documentation, which is basically t

相关标签:
7条回答
  • 2020-12-13 00:09

    Bringing this topic back from the dead to mention that GH now supports redirect-from's redirect-to parameter https://github.com/jekyll/jekyll-redirect-from#redirect-to

    Simply add this to your _config.yml

    gems:
      - jekyll-redirect-from
    

    And this to the top of your index page.

    ---
    redirect_to: "http://example.com"
    ---
    
    0 讨论(0)
  • 2020-12-13 00:17

    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 :

    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Redirecting...</title>
      {% comment %}
        Don't use 'redirect_to' to avoid conflict
        with the page redirection plugin: if that is defined
        it takes over.
      {% endcomment %}
      <link rel="canonical" href="{{ page.redir_to }}"/>
      <meta http-equiv="refresh" content="0;url={{ page.redir_to }}" />
    </head>
    <body>
      <h1>Redirecting...</h1>
      <a href="{{ page.redir_to }}">Click here if you are not redirected.<a>
      <script>location='{{ page.redir_to }}'</script>
    </body>
    </html>
    

    Now:

    firefox localhost:4000/a
    

    will redirect you to example.com.

    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
    

    Tested on GitHub pages v64, live demo at: https://github.com/cirosantilli/cirosantilli.github.io/tree/d783cc70a2e5c4d4dfdb1a36d518d5125071e236/r

    0 讨论(0)
  • 2020-12-13 00:20

    Github pages don't support anything like .htaccess or nginx/conf

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

    so easiest way is:

    HTML redirect:

    index.html

    <html>
      <head>
        <meta http-equiv="refresh" content="0; url=http://www.mywebsite.com/" />
      </head>
    
      <body>
        <p><a href="http://www.mywebsite.com/">Redirect</a></p>
      </body>
    </html>
    
    0 讨论(0)
  • 2020-12-13 00:21

    Why didn't you use http://www.w3.org/TR/WCAG20-TECHS/H76.html?

    That would give

    <meta http-equiv="refresh" content="0;URL='http://casperjs.org/'" />
    
    0 讨论(0)
  • 2020-12-13 00:22

    I had a similar issue when switching the domain for my github pages site. I set up rerouter on Heroku to handle the 301 redirects to the new domain. It handles domain-to-domain redirects very simply, but you may have to modify it to handle your site's legacy domain+path location.

    I described the steps in detail here:

    http://joey.aghion.com/simple-301-redirects/

    0 讨论(0)
  • 2020-12-13 00:26

    To avoid the duplicate content, in a first time you can add a meta canonical like this:

    <link rel="canonical" href="http://casperjs.org">
    
    0 讨论(0)
提交回复
热议问题