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

NiK*_*iKo 47 hosting redirect github jekyll github-pages

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

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

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

与此同时,我已经通过该casperjs.org域名获取此网站,因此我按照CNAME文档中的建议放置了一个文件:https://github.com/n1k0/casperjs/blob/gh-pages/CNAME - in他们的例子,该操作应该从www.example.com和charlie.github.com创建重定向到example.com ...

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

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

Per*_*rry 29

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

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

gems:
  - jekyll-redirect-from
Run Code Online (Sandbox Code Playgroud)

这是索引页面的顶部.

---
redirect_to: "http://example.com"
---
Run Code Online (Sandbox Code Playgroud)

  • 很好的解决方案,但值得指出的是,这个gem创建了一个带有元刷新重定向的html页面,而不是http级重定向. (14认同)
  • 除了@MaximillianLaumeister 之外,这意味着它是一个 http 级别的 200 而不是 301。这意味着您将失去页面的 SEO 价值。 (2认同)
  • 只是提一下..如果您使用“gitlab”而不是“github”,则必须使用“plugins”而不是“gems”。 (2认同)

Jon*_*Lee 19

不。

其他答案讨论了使用元刷新或 javascript 进行重定向。但OP询问了301重定向。答案是:。这不可能。您在 GitHub Pages 上的站点是静态的,因此您对服务器没有任何控制权。


pio*_*ouM 13

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

<link rel="canonical" href="http://casperjs.org">
Run Code Online (Sandbox Code Playgroud)


Gro*_*NaN 8

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

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

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

  • 在此期间,您可以在页面中添加rel ="canonical",因此Google不会将这些网页声明为"重复":http://support.google.com/webmasters/bin/answer.py?hl =恩和答案= 139394 (10认同)
  • 实际上,这是github支持团队提供的官方答案:"这样的重定向目前在Pages上是不可能的.我们可能会在将来添加它.你可以使用一点javascript魔法为你做一个重定向,这就是我们的意思在help.github.com上做." (4认同)

Cir*_*四事件 6

手动排版方式

如果你不喜欢使用https://github.com/jekyll/jekyll-redirect-from,那么自己实现它很容易:

a.md

---
layout: 'redirect'
permalink: /a
redir_to: 'http://example.com'
sitemap: false
---
Run Code Online (Sandbox Code Playgroud)

_layouts/redirect.html基于从 HTML 页面重定向

<!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>
Run Code Online (Sandbox Code Playgroud)

现在:

firefox localhost:4000/a
Run Code Online (Sandbox Code Playgroud)

将您重定向到example.com.

像这个例子一样,redirect-from插件不生成 301,只生成meta+ JavaScript 重定向。

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

curl localhost:4000/a
Run Code Online (Sandbox Code Playgroud)

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


equ*_*nt8 6

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>
Run Code Online (Sandbox Code Playgroud)


vin*_*yll 5

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

那会给

<meta http-equiv="refresh" content="0;URL='http://casperjs.org/'" />
Run Code Online (Sandbox Code Playgroud)