Eleventy: Add support for page aliases

Created on 28 Apr 2019  路  6Comments  路  Source: 11ty/eleventy

I'm create a page for each year of an annual event, so I have obvious page names with the year (2012, 2013, etc.) which is very nice for sorting and discoverability.

However, the event has a codename this year and people often refer to specific events by their theme and are likely to remember the codename without remembering the exact year.

I'd love to be able to provide a "page alias" or something in the frontmatter that would just redirect to the page

Example 2012.md:

title: Cool Year
codename: cool
alias: /event/cool
---
# ...
enhancement needs-votes

Most helpful comment

Hey @vivek-raju,

In my opinion redirects should be handled outside of Eleventy on the server side using 301 or 302 redirects. If you must, you can however do it client side by using meta refresh.

If you need to go this route you could create a reusable template called redirect.njk (assuming njk, you could be using any engine).

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="refresh" content="0; URL='{{ destination }}'" />
  <title>Redirecting to: {{ destination }}</title>
</head>
<body>
  <!-- Redirect page -->
</body>
</html>

You could then create a folder to hold your redirects: (src/_redirects/home.njk):

---
layout: redirect.njk
permalink: "/home/"
destination: "/here/"
---

Again, I'd look into using something outside of Eleventy for this but if you must handle it client side you could go this route.

Could maybe point you in a better direction knowing a bit more about your stack. Where are you hosting Strapi and Eleventy?

All 6 comments

Hmm... looked all over for something containing aliases and couldn't find it. Seconds after posting this I realized I should probably look for "redirect" and found #394 which does discuss redirects (and unfortunately that they might not be possible at the moment.

If you are using Netlify, they have pretty sweet redirect functionalities https://www.netlify.com/docs/redirects/

Ah, that looks like it'll do what I want to do. Now I just have to see about making something generate the redirects from something in the front matter.

This repository is now using lodash style issue management for enhancements. This means enhancement issues will now be closed instead of leaving them open.

View the enhancement backlog here. Don鈥檛 forget to upvote the top comment with 馃憤!

Any redirects plugin available in eleventy? I am not using netlify, I am using eleventy with strapi

Hey @vivek-raju,

In my opinion redirects should be handled outside of Eleventy on the server side using 301 or 302 redirects. If you must, you can however do it client side by using meta refresh.

If you need to go this route you could create a reusable template called redirect.njk (assuming njk, you could be using any engine).

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="refresh" content="0; URL='{{ destination }}'" />
  <title>Redirecting to: {{ destination }}</title>
</head>
<body>
  <!-- Redirect page -->
</body>
</html>

You could then create a folder to hold your redirects: (src/_redirects/home.njk):

---
layout: redirect.njk
permalink: "/home/"
destination: "/here/"
---

Again, I'd look into using something outside of Eleventy for this but if you must handle it client side you could go this route.

Could maybe point you in a better direction knowing a bit more about your stack. Where are you hosting Strapi and Eleventy?

Was this page helpful?
0 / 5 - 0 ratings