question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add a feature to tell Lektor that one url_path should redirect to another path (either a source path or a url path).

Why does this need to be in Lektor core? Because different publishing targets configure redirects in different ways. On GitHub Pages, for example, only HTML meta-refresh redirects are possible. With surge.sh, the redirects can be listed in a top-level file called REDIRECTS. With Apache, each directory’s redirects are listed in an .htaccess file, and so on.

Since the implementation of redirects varies according to publishing target, publisher plugins need a consistent way to read the list of redirects. Therefore I think redirects should be in Lektor core.

Proposal:

1. Redirects can be listed in the project file:

[redirects]
/foo /bar
/one /two 307  # specify the HTTP code
/old source("new")  # specify a source path which will be urlized

Combining this with “alts” might be a little tricky.

2. The dev server implements redirects when you preview your site.

3. The standard publishing targets implement HTML meta-refresh redirects: at each url path that must be redirected, a minimal index.html is generated with:

<meta http-equiv="refresh" content="0; url=/target/" />

4. You can pass a command-line argument to a publisher, or configuration in the project file, to tell the publisher to use an alternate implementation. For example if you’re SFTPing to an Apache server’s /var/www/, the publisher should generate .htaccess files instead of HTML redirects.

lektor deploy --redirect-type apache

5. Publisher plugins can read the list of redirects and the redirect options and make choices about how to implement redirects.

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

3reactions
jordanlevcommented, Jan 31, 2017

I understand that different build targets handle redirects in different ways, but if the “meta refresh” tag is sufficient, it might be worth documenting this very straightforward method of implementing redirects in the CMS (which doesn’t require any new functionality):

  1. Create a new model redirect.ini, with 1 field for the redirect target:
[model]
name = Redirect

[fields.target]
label = Redirect Target
type = url
  1. Create a template for the model that does not extend your design layout, but instead only contains this one line:
<meta http-equiv="refresh" content="0;url={{ target }}">

Now you can just add new “pages” of this model type in your sitemap – very simple, and should work 99% of the time (until the more robust feature mentioned in this github issue is implemented).

1reaction
xlotlucommented, Feb 4, 2020

For local assets it makes sense, but sometimes you might want to reditect to an external page because reasons…

I think external redirects and internal redirects are different use-cases.

External redirects seem to me “content items”, which are best served by @jordanlev’s approach above with a custom model.

Internal redirects on the other hand are metadata of existing content. (“This is content that moved around”, or “I want this short url for the current page”). Using a separate model is messy, like @adrianharabula noted, and one could also make the case it’s structurally incorrect – you lose the relationship between the source url and the target node.

If someone creates a plugin that generates simple html files with meta refresh that would definitely be useful. After that proof of concept, it probably would be a good addition to core, with the note that then we’d have to support pluggable backends to generate .htaccess or nginx confs or whatnot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redirects: Different types and how to implement them - Moz
A redirect is a way to send both users and search engines to a different URL from the one they originally requested. The...
Read more >
The Ultimate Guide to Redirects: URL Redirections Explained
Redirects send users from one URL to another. The first URL is the one the user clicked, typed in, or otherwise requested. The...
Read more >
Redirects and Google Search | Documentation
Redirecting URLs is the practice of resolving an existing URL to a different one, effectively telling your visitors and Google Search that a ......
Read more >
URL redirection - Wikipedia
A redirect service is an information management system, which provides an internet link that redirects users to the desired content. · The first...
Read more >
HTTP redirect codes for SEO explained - ContentKing
The HTTP redirect code, redirect for short, is a way to forward visitors and search engines from one URL to another. Redirects are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found