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

问题描述 投票:70回答:8

我在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

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

redirect github jekyll http-status-code-301 github-pages
8个回答
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/" />

使用<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


21
投票

你试过Jekyll Alias Generator plugin吗?

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

---
  layout: post
  title: "My Post With Aliases"
  alias: [/first-alias/index.html, /second-alias/index.html]
---

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

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

另见this blog post关于这个问题。


11
投票

This solution allows you to use true HTTP redirects via .htaccess — however, nothing involving .htaccess will work on GitHub pages because they do not use Apache.

截至2014年5月GitHub Pages supports redirects,但根据jekyll-redirect-from Gem documentation,他们仍然基于HTTP-REFRESH(使用<meta>标签),这需要在重定向之前完全加载页面。

我不喜欢<meta>方法所以我为那些希望使用Apache提供真正的HTTP 301重定向的人提供了一个解决方案,该服务器提供预生成的Jekyll站点:


首先,将.htaccess添加到include_config.yml属性中

include: [.htaccess]

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

---
---
DirectoryIndex index.html

RewriteEngine On
RewriteBase /

...

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

---
permalink: /my-new-path/
original: blog/my/old/path.php
---

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

{% for post in site.categories.post %}
  RewriteRule ^{{ post.original }} {{ post.permalink }} [R=301,L]
{% endfor %}

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

RewriteRule ^blog/my/old/path.php /my-new-path/ [R=301,L]

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

ln -s /path/to/my-blog/_site /var/www/vhosts/my-blog.com

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

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


8
投票

redirect-from插件

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

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

_config.yml

gems:
  - jekyll-redirect-from

啊.面对

---
permalink: /a
redirect_to: 'http://example.com'
---

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

现在:

firefox localhost:4000/a

将你重定向到example.com

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

在GitHub页面v64上测试。

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

手动布局方法

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

啊.面对

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

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

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

curl localhost:4000/a

5
投票

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

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

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


2
投票

由于github不允许301重定向(这并不奇怪),因此您必须在转移到新的URL结构(并采用搜索引擎命中)或保留URL之间做出决定。我建议你继续前进。让搜索引擎芯片落在他们可能的地方。如果有人通过搜索引擎点击您的旧链接,他们将被重定向到新位置。随着时间的推移,搜索引擎将收集您的更改。

你可以做的事情就是创建一个Sitemap,你只列出你的新页面而不是旧页面。这应该加快旧URL的替换。此外,如果所有旧URL都在“/ programovani”目录中,您还可以使用robots.txt file告诉将来的抓取,他们应该忽略该目录。例如:

User-agent: *
Disallow: /programovani/

搜索引擎需要一段时间才能赶上变化。这不是什么大不了的事。只要旧URL仍然存在并将实际人员重定向到活动页面,您就可以了。


1
投票

正如其他人所提到的,最好的解决方案是保留工作URL或复制页面并指定canonical URL。

由于github页面不支持真正的重定向,因此我选择在Heroku上设置rerouter,以便将301(永久)重定向从我的站点的旧域重新定向到新域。我在这里描述了细节:

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


1
投票

Jekyll在过去几个月里经历了一些重大更新,所以当这个问题最初发布时,这可能是不可能的......

Jekyll在你博客文章的permalink中支持YAML front-matter section属性。您可以在生成站点时指定您希望帖子具有的URL,Jekyll将使用该URL(而不是文件名)。

---
title: My special blog post
permalink: /programovani/2010/04/git-co-to-je-a-co-s-tim
---
My blog post markdown content
© www.soinside.com 2019 - 2024. All rights reserved.