How to Add Redirects to Post URL on Octopress
When I write a new post on Octopress, I share the link of it to somewhere doesn’t support hyperlinks. Since people can’t click the link, they should copy and paste or just type it letter by letter. I wanted to make it easier, so I maded short url for every post.
Alias Generator for Posts
There is a Jekyll plugin that generates redirect pages for posts with aliases. Octopress is based on Jekyll and this plugin has no compatibility problem. Its source is on GitHub, so I just added it as submodule:
$ git submodule add git@github.com:tsmango/jekyll_alias_generator plugins/jekyll_alias_generator
In your _config.yml
, you may have this line:
plugins: plugins
Then it reads plugins
directory and alias_generator.rb
in plugins/jekyll_alias_generator/_plugins/
directory is also loaded, so you can use and manage it!
How to Use
This plugin checks alias
inside every post’s YAML Front Matter. Just place the path of the alias:
---
layout: post
title: "How to Add Redirects to Post URL on Octopress"
alias: /p/20140523
---
Multiple aliases are also available:
---
layout: post
title: "How to Add Redirects to Post URL on Octopress"
alias: [/one-alias/index.html, /another-alias/index.html]
---
When I rake generate
, the plugin generates static html file at /p/20140523/index.html
:
<!DOCTYPE html>
<html>
<head>
<link rel="canonical" href="/2014/05/23/how-to-add-redirects-to-post-url-on-octopress/"/>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;url=/2014/05/23/how-to-add-redirects-to-post-url-on-octopress/" />
</head>
</html>
When you go to /p/20140523, it will redirect here. It also has canonical link, so it won’t affect search engine or web analysis services.