首页 文章

301重定向为github托管的网站?

提问于
浏览
41

这是我的Github存储库:https://github.com/n1k0/casperjs

有一个 gh-pages 分支来保存项目文档,基本上是项目网站:https://github.com/n1k0/casperjs/tree/gh-pages

该分支设置文档站点http://n1k0.github.com/casperjs/ - hurray .

与此同时,我已经通过它获取了这个网站,因此我将 CNAME 文件作为recommended in the docshttps://github.com/n1k0/casperjs/blob/gh-pages/CNAME - 在他们的示例中,该操作应该从www.example.com和charlie创建重定向 . github.com到example.com ...

虽然该网站现在指向http://casperjs.org/,但是从http://n1k0.github.com/casperjs/(旧网站网址)到新域名没有301重定向 .

知道如何设置这样的重定向,如果可能的话?这是一个错误吗?如果是,我应该在哪里开一个问题?

7 回答

  • 1

    将这个话题从死里复活,提到GH现在支持重定向 - 从重定向到参数https://github.com/jekyll/jekyll-redirect-from#redirect-to

    只需将其添加到_config.yml即可

    gems:
      - jekyll-redirect-from
    

    这是索引页面的顶部 .

    ---
    redirect_to: "http://example.com"
    ---
    
  • 6

    为避免重复内容,您可以在第一时间添加如下的元规范:

    <link rel="canonical" href="http://casperjs.org">
    
  • 2

    您可以在主机检测后使用Javascript重定向,如下所示:

    if (window.location.href.indexOf('http://niko.github.com') === 0) {
        window.location.href = 'http://casperjs.org{{ page.url }}';
    }
    

    但我同意,这不是HTTP重定向 .

  • 12

    Manual layout method

    如果您不想使用https://github.com/jekyll/jekyll-redirect-from,您可以自行轻松实现:

    a.md

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

    _layouts/redirect.html 基于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>
    

    现在:

    firefox localhost:4000/a
    

    将您重定向到 example.com .

    与此示例类似, redirect-from 插件不生成301s,只有 meta JavaScript重定向 .

    我们可以验证发生了什么:

    curl localhost:4000/a
    

    在GitHub页面v64上测试,现场演示:https://github.com/cirosantilli/cirosantilli.github.io/tree/d783cc70a2e5c4d4dfdb1a36d518d5125071e236/r

  • 2

    你为什么不用http://www.w3.org/TR/WCAG20-TECHS/H76.html

    这会给

    <meta http-equiv="refresh" content="0;URL='http://casperjs.org/'" />
    
  • 28

    Github页面不支持 .htaccessnginx/conf 之类的任何内容

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

    最简单的方法是:

    HTML重定向:

    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>
    
  • 4

    在为我的github页面网站切换域时,我遇到了类似的问题 . 我在Heroku上设置rerouter来处理301重定向到新域 . 它可以非常简单地处理域到域的重定向,但您可能需要对其进行修改以处理站点的旧域路径位置 .

    我在这里详细描述了这些步骤:

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

相关问题