59

Here's a Github repository of mine: https://github.com/n1k0/casperjs

There's a gh-pages branch to hold the project documentation, which is basically the project website: https://github.com/n1k0/casperjs/tree/gh-pages

This branch setups the documentation site at http://n1k0.github.com/casperjs/ — hurray.

In the meanwhile, I've bough the casperjs.org domain to get this website available through it, so I put a CNAME file as recommended in the docs: https://github.com/n1k0/casperjs/blob/gh-pages/CNAME — in their example, the operation is supposed to create redirects from www.example.com and charlie.github.com to example.com

While the website now points to http://casperjs.org/, there's no 301 redirect from http://n1k0.github.com/casperjs/ (the old site url) to the new domain name.

Any idea how to setup such a redirect, if it's even possible? Is it a bug? If it is, where should I open an issue?

NiKo
  • 10,824
  • 5
  • 44
  • 56
  • 2
    +1. I'd also rather my page was _only_ accessible through the domain name. – James McLaughlin Feb 14 '12 at 12:26
  • 2
    possible duplicate of [Permanent redirect from Github gh-pages](http://stackoverflow.com/questions/5302663/permanent-redirect-from-github-gh-pages) – Fred Foo Feb 14 '12 at 12:26
  • ah, I swear I've been thoroughly searching before asking, I'm sorry :$ Edit: quite not the same question actually, and some insightful comments have been posted below methinks :) – NiKo Feb 14 '12 at 13:20
  • Possible duplicate of [What is the best approach for redirection of old pages in Jekyll and GitHub Pages?](http://stackoverflow.com/questions/10178304/what-is-the-best-approach-for-redirection-of-old-pages-in-jekyll-and-github-page) – Ciro Santilli Путлер Капут 六四事 Apr 25 '16 at 18:34

7 Answers7

40

Bringing this topic back from the dead to mention that GH now supports redirect-from's redirect-to parameter https://github.com/jekyll/jekyll-redirect-from#redirect-to

Simply add this to your _config.yml

gems:
  - jekyll-redirect-from

And this to the top of your index page.

---
redirect_to: "http://example.com"
---
Perry
  • 834
  • 8
  • 13
15

To avoid the duplicate content, in a first time you can add a meta canonical like this:

<link rel="canonical" href="http://casperjs.org">
piouPiouM
  • 4,712
  • 1
  • 18
  • 22
  • 1
    Ah yes I did forget this one, thank you for the hint :) But that doesn't solve the main issue which is the lack of permanent redirect, unfortunately :/ – NiKo Feb 14 '12 at 12:51
8

You can redirect using Javascript after host detection, like this:

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

But I agree, it's not an HTTP redirection.

Michael Gaskill
  • 7,738
  • 10
  • 38
  • 42
GromNaN
  • 572
  • 5
  • 7
  • 1
    Problem is that it won't solve the "google duplicate contents" issue I'm encountering right now… :/ – NiKo Feb 14 '12 at 12:30
  • 10
    In the meantime, you can add a rel="canonical" to your page, so Google won't declare those pages as "duplicate" : http://support.google.com/webmasters/bin/answer.py?hl=en&answer=139394 – cyberdelia Feb 14 '12 at 12:46
  • 4
    Actually, that's the official answer provided by the github support team: "Redirects like that aren't currently possible on Pages. We might add it in the future. You could use a little javascript magic to do a redirect for you, that's what we do on help.github.com." – NiKo Feb 15 '12 at 19:33
  • Note: this kind of "solution" is not without issues. Read http://www.w3.org/QA/Tips/reback – Denilson Sá Maia Jan 31 '14 at 14:05
6

Why didn't you use http://www.w3.org/TR/WCAG20-TECHS/H76.html?

That would give

<meta http-equiv="refresh" content="0;URL='http://casperjs.org/'" />
vinyll
  • 10,499
  • 2
  • 42
  • 36
  • HTML redirect is a good way, thanks for your answer but it is not a permanent way. I upvote you anyway. –  Jun 17 '19 at 05:59
6

Manual layout method

If you don't feel like using https://github.com/jekyll/jekyll-redirect-from it's easy to implement it yourself:

a.md:

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

_layouts/redirect.html based on 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>

Now:

firefox localhost:4000/a

will redirect you to example.com.

Like this example, the redirect-from plugin does not generate 301s, only meta + JavaScript redirects.

We can verify what is going on with:

curl localhost:4000/a

Tested on GitHub pages v64, live demo at: https://github.com/cirosantilli/cirosantilli.github.io/tree/d783cc70a2e5c4d4dfdb1a36d518d5125071e236/r

6

Github pages don't support anything like .htaccess or nginx/conf

https://help.github.com/articles/redirects-on-github-pages/

so easiest way is:

HTML redirect:

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>
equivalent8
  • 12,962
  • 7
  • 77
  • 103
1

I had a similar issue when switching the domain for my github pages site. I set up rerouter on Heroku to handle the 301 redirects to the new domain. It handles domain-to-domain redirects very simply, but you may have to modify it to handle your site's legacy domain+path location.

I described the steps in detail here:

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

Joey
  • 439
  • 2
  • 6