59

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

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

这个分支在http://n1k0.github.com/casperjs/上设置了文档站点——万岁。

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

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

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

4

7 回答 7

39

把这个话题起死回生提到GH现在支持redirect-from的redirect-to参数https://github.com/jekyll/jekyll-redirect-from#redirect-to

只需将其添加到您的 _config.yml

gems:
  - jekyll-redirect-from

这在您的索引页面的顶部。

---
redirect_to: "http://example.com"
---
于 2015-09-08T19:23:58.157 回答
15

为了避免重复的内容,您可以在第一次添加这样的元规范:

<link rel="canonical" href="http://casperjs.org">
于 2012-02-14T12:48:33.407 回答
8

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

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

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

于 2012-02-14T12:29:44.450 回答
6

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

那会给

<meta http-equiv="refresh" content="0;URL='http://casperjs.org/'" />
于 2014-05-05T10:39:53.307 回答
6

手动布局方法

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

a.md

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

_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>

现在:

firefox localhost:4000/a

会将您重定向到example.com.

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

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

curl localhost:4000/a

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

于 2016-04-25T17:00:42.803 回答
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>
于 2017-05-25T08:29:27.870 回答
1

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

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

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

于 2013-07-16T03:41:03.990 回答