20

I moved my site http://www.phonations.com to Jekyll/GitHub pages.

Before that, I knew how to redirect some urls on my site to external ones. How to do that now?

For example, I would like http://www.phonations.com/tmwtga to redirect to https://www.dropbox.com/s/tw1b0kvoc75c7lx/tmwtga.zip?dl=0 or some other url (using git lfs).

While the plugin "jekyll-redirect-from" allows redirecting a path to a page, it does not seem to allow redirecting a path to another kind of resource.

Samuel Lelièvre
  • 3,212
  • 1
  • 14
  • 27
Martin Delille
  • 11,360
  • 15
  • 65
  • 132
  • What exactly do you mean when you say it doesn't seem to do the job? What exactly have you tried? What exactly happened when you tried it? Are you using GitHub Pages, or something else? Are you encountering any errors, either in the build process or in the browser when you load the page? – Kevin Workman Sep 23 '16 at 15:16
  • The plugin makes possible to redirect a url to a page but not to another url – Martin Delille Sep 23 '16 at 17:26
  • I'm using github pages. – Martin Delille Sep 23 '16 at 17:26
  • I just post my answer here: https://stackoverflow.com/questions/56067700/redirect-to-page-in-jekyll/56067716#56067716 – piecioshka May 09 '19 at 21:20
  • Does this answer your question? [What is the best approach for redirection of old pages in Jekyll and GitHub Pages?](https://stackoverflow.com/questions/10178304/what-is-the-best-approach-for-redirection-of-old-pages-in-jekyll-and-github-page) – Ciro Santilli OurBigBook.com Apr 21 '20 at 07:40
  • No my question is not about redirecting content. I wanted to make available with a simple link an asset (here on dropbox). The accepted answer is cute, fine and totally in the Jekyll mindset. – Martin Delille Apr 23 '20 at 11:47

2 Answers2

31

That plugin does allow for redirecting to external URLs. To redirect your example URL, create a file called tmwtga.html and add the following front matter:

---
permalink: /tmwtga
redirect_to:
  - http://www.example.com
---

Assumes your server supports extensionless URLs (in this case GitHub Pages does). You might not need the permalink, but I put it in there in case you have a default set in _config.yml.

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

Ross
  • 2,701
  • 16
  • 25
17

In case

  • it is OK to use the wrong HTTP response code (200 OK instead of 301 Moved Permanently),
  • you are hosted in GitHub pages,
  • you want to rely on the build-in features, and
  • you do not want to use GitHub workflows for building and pushing to gh-pages then

you can do following:

  1. Create file _layouts/redirect.html:

     <html>
     <head>
         <meta charset="utf-8"/>
         <meta http-equiv="refresh" content="1;url={{ page.redirect }}"/>
         <link rel="canonical" href="{{ page.redirect }}"/>
         <script type="text/javascript">
                 window.location.href = "{{ page.redirect }}"
         </script>
         <title>Page Redirection</title>
     </head>
     <body>
     If you are not redirected automatically, follow <a href='{{ page.redirect }}'>this link</a>.
     </body>
     </html>
    
  2. For each page, you want to redirect from, use following content:

     ---
     redirect:   https://www.example.org
     layout:     redirect
     ---
    

The HTML code is an extention to the HTML code provided by a tutorial by W3C.

koppor
  • 19,079
  • 15
  • 119
  • 161
  • 2
    It's worth noting that this is essentially reimplementing the plugin. See the template that jekyll-redirect-from uses here: https://github.com/jekyll/jekyll-redirect-from/blob/master/lib/jekyll-redirect-from/redirect.html – Ross Nov 15 '20 at 20:12