在Jekyll和GitHub页面中重定向旧页面的最佳方法是什么?

Mai*_*tel 71 redirect github jekyll http-status-code-301 github-pages

我在github页面上有博客 - jekyll

解决URL策略迁移的最佳方法是什么?

我发现最常见的做法就是创建像这样的htaccess

Redirect 301 /programovani/2010/04/git-co-to-je-a-co-s-tim/ /2010/04/05/git-co-to-je-a-co-s-tim.html
Run Code Online (Sandbox Code Playgroud)

但它似乎与Github无关.我找到的另一个解决方案是创建rake任务,它将生成重定向页面.但由于它是一个html,它无法发送301头,因此SE爬虫不会将其识别为重定向.

Kon*_*ski 63

最好的解决方案是同时使用<meta http-equiv="refresh"<link rel="canonical" href=

效果非常好,谷歌博特重新索引我的整个网站在新的链接下,而不会失去职位.用户也会立即重定向到新帖子.

<meta http-equiv="refresh" content="0; url=http://konradpodgorski.com/blog/2013/10/21/how-i-migrated-my-blog-from-wordpress-to-octopress/">
<link rel="canonical" href="http://konradpodgorski.com/blog/2013/10/21/how-i-migrated-my-blog-from-wordpress-to-octopress/" />
Run Code Online (Sandbox Code Playgroud)

使用<meta http-equiv="refresh"会将每个访问者重定向到新帖子.至于谷歌博特,它被视为<link rel="canonical" href=301重定向,效果是你的页面重新索引,这就是你想要的.

我描述了整个过程如何将我的博客从Wordpress移到Octopress. http://konradpodgorski.com/blog/2013/10/21/how-i-migrated-my-blog-from-wordpress-to-octopress/#redirect-301-on-github-pages

  • 转移到GitHub页面时,这对我有用:https://help.github.com/articles/redirects-on-github-pages.它看起来像你提到的一切. (5认同)
  • @ ErikBerkun-Drevnig上面看到的内容被添加到"旧"页面上,应该指向"新"页面.做到这一点,不应该有一个无限循环. (3认同)

ms-*_*ati 21

你有没有尝试过Jekyll Alias Generator插件

你把别名网址放在帖子的YAML前面:

---
  layout: post
  title: "My Post With Aliases"
  alias: [/first-alias/index.html, /second-alias/index.html]
---
Run Code Online (Sandbox Code Playgroud)

当用户访问其中一个别名网址时,会通过元标记刷新将其重定向到主网址:

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <meta http-equiv="refresh" content="0;url=/blog/my-post-with-aliases/" />
  </head>
</html>
Run Code Online (Sandbox Code Playgroud)

另请参阅此博客文章.

  • GitHub Pages不使用插件 (3认同)
  • 我最终遇到了这样的事情。我通过Rake任务在本地生成重定向页面,并将其作为静态页面推送到Github (2认同)

Chr*_*pel 11

此解决方案允许您通过.htaccess使用真正的HTTP重定向 - 但是,任何涉及.htaccess的内容都不适用于GitHub页面,因为它们不使用Apache.

截至2014年5月,GitHub Pages支持重定向,但根据geek文档中jekyll-redirect,它们仍然基于HTTP-REFRESH(使用<meta>标签),这需要在重定向之前完全加载页面.

我不喜欢这种<meta>方法所以我为那些希望使用Apache在.htaccess文件中提供真正的HTTP 301重定向的人提供了一个解决方案,它为预先生成的Jekyll站点提供服务:


首先,添加.htaccess到该include属性中_config.yml

include: [.htaccess]
Run Code Online (Sandbox Code Playgroud)

接下来,创建一个.htaccess文件,并确保包含YAML前端内容.这些破折号很重要,因为现在Jekyll将用Liquid,Jekyll的模板语言解析文件:

---
---
DirectoryIndex index.html

RewriteEngine On
RewriteBase /

...
Run Code Online (Sandbox Code Playgroud)

确保您需要重定向的帖子有两个属性,如下所示:

---
permalink: /my-new-path/
original: blog/my/old/path.php
---
Run Code Online (Sandbox Code Playgroud)

现在在.htaccess中,只需添加一个循环:

{% for post in site.categories.post %}
  RewriteRule ^{{ post.original }} {{ post.permalink }} [R=301,L]
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

这将在每次构建站点时动态生成.htaccess,并且include在配置文件中确保.htaccess使其进入_site目录.

RewriteRule ^blog/my/old/path.php /my-new-path/ [R=301,L]
Run Code Online (Sandbox Code Playgroud)

从那里开始,您可以_site使用Apache 进行服务.我通常将完整的Jekyll repo克隆到非webroot目录中,然后我的vhost是该_site文件夹的符号链接:

ln -s /path/to/my-blog/_site /var/www/vhosts/my-blog.com
Run Code Online (Sandbox Code Playgroud)

田田!现在,Apache可以从虚拟根目录提供_site文件夹,并使用.htaccess驱动的重定向,使用您想要的任何HTTP响应代码!

您甚至可以获得超级幻想,并redirect在每个帖子的前端内部使用属性来指定在.htaccess循环中使用哪个重定向代码.

  • 当您生成.htaccess文件时,该解决方案将涉及更复杂的逻辑。例如,您可以转换YAML,以使“原始”是数组而不是字符串。然后,您需要一个嵌套循环,以便每个“原始”条目都会生成一个到“永久链接”的重定向。以此代码为起点,自己进行试验! (2认同)
  • 由于此解决方案在 GitHub 页面上不起作用,因此它无法回答任何问题。不相关的答案数量是无限的,那么为什么要发布这个呢? (2认同)

Cir*_*四事件 8

redirect-from插件

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

它由GitHub支持并使其变得简单:

_config.yml

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

a.md

---
permalink: /a
redirect_to: 'http://example.com'
---
Run Code Online (Sandbox Code Playgroud)

如下所述:https://help.github.com/articles/redirects-on-github-pages/

现在:

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

将重定向到你example.com.

只要redirect_to页面定义,插件就会接管.

在GitHub页面v64上测试.

注意:此版本有一个严重的最近修复的错误,错误地重用了重定向的默认布局:https://github.com/jekyll/jekyll-redirect-from/pull/106

手动布局方法

如果您不想使用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)

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

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

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


Tom*_*son 5

最好的选择是通过在_config.yml中设置永久链接格式来匹配旧博客,从而完全避免网址更改.

除此之外,最完整的解决方案是生成重定向页面,但不一定值得付出努力.我最终只是让我的404页面更友好,使用javascript来猜测正确的新网址.它对搜索没有任何作用,但实际用户可以访问他们正在寻找的页面,并且在其余代码中没有可支持的遗留内容.

http://tqcblog.com/2012/11/14/custom-404-page-for-a-github-pages-jekyll-blog/