Add support for page redirects/aliases (👍15) #3988
Replies: 9 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. |
Beta Was this translation helpful? Give feedback.
-
|
If you are using Netlify, they have pretty sweet redirect functionalities https://www.netlify.com/docs/redirects/ |
Beta Was this translation helpful? Give feedback.
-
|
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. |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
|
Any redirects plugin available in eleventy? I am not using netlify, I am using eleventy with strapi |
Beta Was this translation helpful? Give feedback.
-
|
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 <!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: ( ---
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? |
Beta Was this translation helpful? Give feedback.
-
|
I've carried this a little bit further, and found a way to do declaration of several redirections/aliases in a single file -- which can help things be a bit more organizationally scalable if you've got quite a few of them: In some file such as ---
# This file does hijinx with the "pagingation" system to generate many small pages from one set of data...
# and uses that do to redirects from some URLs to others.
# We use this to try to keep old links working.
#
# There's limited power to this approach (it only works for specific pages listed; it can't glob),
# but those are limitations inherent to an approach that works via static site gen, rather than via server configuration.
# The related upside of an approach that works via static site gen is the portability.
pagination:
data: redirects
size: 1
alias: redirect
# Add your redirection tuples to this list!
redirects:
- {"from": "/x/y/", "to": "/foo/"}
- {"from": "/x/z/", "to": "/bar/"}
# The "permalink" attribute determines where the output page will be located.
permalink: "{{ redirect.from }}"
# The "redirect" layout just has a small html header with the meta tags that do redirection.
layout: redirect
---
(the content can be left blank; it's entirely the frontmatter doing the work here.)And in <!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="0; url='{{ redirect.to }}'" />
</head>
<body>
you should be redirecting to <a href="{{ redirect.to }}">{{ redirect.to }}</a>
</body>
</html>It's possible this might be considered "obvious" if one has already wrapped their head around 11ty's pagination and data flow concepts, but it took me a bit to piece it all together, so I thought it might be useful to share this worked solution. Hope it helps someone out there. :) |
Beta Was this translation helpful? Give feedback.
-
|
Amazing solution @warpfork |
Beta Was this translation helpful? Give feedback.
-
|
I found this trick as a semi drop-in replacement for Jekyll's redirect_from: |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
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:Beta Was this translation helpful? Give feedback.
All reactions