Name | lektor-redirect JSON |
Version |
0.1.0b2
JSON |
| download |
home_page | None |
Summary | A Lektor plugin to help with generating redirects for, e.g., moved pages. |
upload_time | 2024-08-18 17:39:13 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.8 |
license | MIT |
keywords |
lektor
plugin
|
VCS |
|
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
# lektor-redirect
[![PyPI version](https://img.shields.io/pypi/v/lektor-redirect.svg)](https://pypi.org/project/lektor-redirect/)
[![PyPI Supported Python Versions](https://img.shields.io/pypi/pyversions/lektor-redirect.svg)](https://pypi.python.org/pypi/lektor-redirect/)
[![GitHub license](https://img.shields.io/github/license/dairiki/lektor-redirect)](https://github.com/dairiki/lektor-redirect/blob/master/LICENSE)
[![GitHub Actions (Tests)](https://img.shields.io/github/actions/workflow/status/dairiki/lektor-redirect/ci.yml?branch=master&label=tests)](https://github.com/dairiki/lektor-redirect/)
This plugin allows pages (and attachments) to specify alternative/old
URLs which should redirect to their current location.
> [!IMPORTANT]
> Currently this plugin *will not work* on Lektor sites with [alternatives] enabled.
## How it works
The plugin looks for a field named (by default) `redirect_from` on pages
and attachments in the site. This field is expected to contain a sequence of URLs
to redirect from.
There are two ways that redirects may be implemented by this plugin.
Either or both may be enabled.
### Redirect pages
Redirect pages can be generated at the specified URLs. The template for these
pages is up to you, however the intent is that these pages will attempt
an _[meta refresh]_ and/or _[javascript redirect]_ to the target page.
### Redirect map
A _redirect map_ file can be generated. This may be used to configure your
web server to issue the desired redirects itself.
(Currently only an nginx-style map is supported.)
## Usage
By default the plugin looks for a field named `redirect_from` on pages in the
site.
(The name of the field may be customized in the plugin configuration file. See below.)
This field should contain a sequence of URLs to redirect from — most
likely it should have a field type of `strings`.
E.g. To be able to generate redirects to your pages, you might add the
following [field][field config] to your `models/page.ini` file:
```ini
[fields.redirect_from]
label = Redirect From
description = Other URLs which should redirect to this page
type = strings
```
The URLs in the `redirect_from` field may either be absolute (beginning)
with a slash (these are interpreted relative to the root of the site) or
they may be relative, in which case they are interpreted relative to the
URL of the parent of the page containing the `redirect_from` field.
As an example, if the is a page at lektor path `/blog/first-post`,
who’s URL, if nothing exotic is done with slug configuration is
`/blog/first-post/`, then, if `first-posts`’s `redirect_from` is set
to `test-post`, then:
- If redirect page generation is enabled, there will be an artifact
generated at in `/blog/test-post/index.html` which will, hopefully,
redirect the user to `/blog/first-post/`.
- If redirect map generation is enabled, it will include an entry
mapping `/blog/test-post` to `/blog/first-post/`.
### Configuration File
The plugin's configuration file is `configs/redirect.ini`.
Settings should be in a `[redirect]` section.
There are currently three configurable settings. Here is an example:
```ini
[redirect]
# The name of the field from which redirects are extracted.
# The default value is "redirect_from"
redirect_from_field = redirect_from
# Set template used to render redirect pages.
# There is no default value — if no template is set, redirect
# page generation is disabled.
template = redirect.html
# Set the name of the redirect map file.
# There is no default value — if no value is set, redirect
# map generation is disabled.
map_file = .redirect.map
```
### Redirect Pages
If a `template` is configured in the plugin configuration file (`configs/redirect.ini`),
_redirect pages_ will be generated from the specified template. The intention is that
the resulting page will redirect the user to the target location using [meta refresh]
and/or a [javascript redirect].
Within the template, the target of the redirect is available as `this.target`.
An simple example for such a template is:
```jinja
<!doctype html>
<html>
<head>
<title>Page Moved</title>
<link rel="canonical" href="{{ this.target|url(external=true) }}">
<!-- meta refresh redirect -->
<meta http-equiv="refresh" content="0; url={{ this.target|url(absolute=true) }}">
<!-- javascript redirect -->
<script type="text/javascript">
window.location.href = {{ this.target|url(absolute=true)|tojson }};
</script>
</head>
<body>
<h1>Page Moved</h1>
<p>
If you are not automatically redirected, the page you want can be found at
<a href="{{ this.target|url }}">{{ this.target|url(external=true) }}</a>.
</p>
</body>
</html>
```
> [!TIP]
> For the `url(external=true)` and `url(absolute=true)`
filters to work, a `[url][project config]` may need to be configured
for the project.
When redirecting from URLs that do not end with `.html` or `.htm`, the redirect page
is generated at the url with `/index.html` appended.
For example if there is a redirect from `/old-image.png` to
`/new-image.png`, the redirect page will be generated at
`/old-image.png/index.html`.
This is done with the hope that the web server, without extra
configuration, will respond to a request for `/old-image.png` with a
content-type header of `text/html`.
### Redirect Map
If a `map_file` is configured in the plugin configuration file (`configs/redirect.ini`),
a *map file* will be generated in the output tree.
The map file is in a format suitable for inclusion in an *nginx* [map block][nginx map].
Assuming there is a single redirect from `/old-page` to `/new-page`, the contents
of the map file would be:
```
/old-page/ /replacement-page/;
```
Assuming that `map_file` is set to `.redirect.map`, the salient parts
of an *nginx* configuration file that utilizes the redirect map might
look like:
```nginx
[...]
http {
[...]
# You may need to adjust this (and/or map_hash_max_size) to avoid
# "could not build map_hash, you should increase map_hash_bucket_size"
# error from nginx
map_hash_bucket_size 128;
map $uri $redirect_to_uri {
default "";
include /path/to/htdocs/.redirect.map;
}
server {
listen [...];
[...];
root /path/to/htdocs;
location ~ /\. {
# Don't serve dot-files (like .redirect.map)
return 404;
}
if ($redirect_to_uri) {
# pass query args to preserve utm_* tracking parameters, etc.
return 301 $redirect_to_uri$is_args$args;
}
[...]
}
}
```
## To Do
- Make this work for Lektor projects with [alternatives] enabled.
- Add support for writing the redirect map file in other formats.
(E.g. Apache [text map][apache text map] format.)
[alternatives]: https://www.getlektor.com/docs/content/alts/
[meta refresh]: https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh
[javascript redirect]: https://developers.google.com/search/docs/crawling-indexing/301-redirects#jslocation
[project config]: https://www.getlektor.com/docs/project/file/#project
[nginx map]: https://nginx.org/en/docs/http/ngx_http_map_module.html
[field config]: https://www.getlektor.com/docs/models/#fields
[apache text map]: https://httpd.apache.org/docs/current/rewrite/rewritemap.html#txt
## Author
Jeff Dairiki <dairiki@dairiki.org>
Raw data
{
"_id": null,
"home_page": null,
"name": "lektor-redirect",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": "lektor, plugin",
"author": null,
"author_email": "Jeff Dairiki <dairiki@dairiki.org>",
"download_url": "https://files.pythonhosted.org/packages/5e/16/264743974b3b38d1d322672c44279141e78a65d16cdd0ca6fbbc82108c59/lektor_redirect-0.1.0b2.tar.gz",
"platform": null,
"description": "# lektor-redirect\n\n[![PyPI version](https://img.shields.io/pypi/v/lektor-redirect.svg)](https://pypi.org/project/lektor-redirect/)\n[![PyPI Supported Python Versions](https://img.shields.io/pypi/pyversions/lektor-redirect.svg)](https://pypi.python.org/pypi/lektor-redirect/)\n[![GitHub license](https://img.shields.io/github/license/dairiki/lektor-redirect)](https://github.com/dairiki/lektor-redirect/blob/master/LICENSE)\n[![GitHub Actions (Tests)](https://img.shields.io/github/actions/workflow/status/dairiki/lektor-redirect/ci.yml?branch=master&label=tests)](https://github.com/dairiki/lektor-redirect/)\n\nThis plugin allows pages (and attachments) to specify alternative/old\nURLs which should redirect to their current location.\n\n> [!IMPORTANT]\n> Currently this plugin *will not work* on Lektor sites with [alternatives] enabled.\n\n## How it works\n\nThe plugin looks for a field named (by default) `redirect_from` on pages\nand attachments in the site. This field is expected to contain a sequence of URLs\nto redirect from.\n\nThere are two ways that redirects may be implemented by this plugin.\nEither or both may be enabled.\n\n### Redirect pages\n\nRedirect pages can be generated at the specified URLs. The template for these\npages is up to you, however the intent is that these pages will attempt\nan _[meta refresh]_ and/or _[javascript redirect]_ to the target page.\n\n### Redirect map\n\nA _redirect map_ file can be generated. This may be used to configure your\nweb server to issue the desired redirects itself.\n(Currently only an nginx-style map is supported.)\n\n\n## Usage\n\nBy default the plugin looks for a field named `redirect_from` on pages in the\nsite.\n(The name of the field may be customized in the plugin configuration file. See below.)\nThis field should contain a sequence of URLs to redirect from \u2014 most\nlikely it should have a field type of `strings`.\n\nE.g. To be able to generate redirects to your pages, you might add the\nfollowing [field][field config] to your `models/page.ini` file:\n\n```ini\n[fields.redirect_from]\nlabel = Redirect From\ndescription = Other URLs which should redirect to this page\ntype = strings\n```\n\nThe URLs in the `redirect_from` field may either be absolute (beginning)\nwith a slash (these are interpreted relative to the root of the site) or\nthey may be relative, in which case they are interpreted relative to the\nURL of the parent of the page containing the `redirect_from` field.\n\nAs an example, if the is a page at lektor path `/blog/first-post`,\nwho\u2019s URL, if nothing exotic is done with slug configuration is\n`/blog/first-post/`, then, if `first-posts`\u2019s `redirect_from` is set\nto `test-post`, then:\n\n- If redirect page generation is enabled, there will be an artifact\n generated at in `/blog/test-post/index.html` which will, hopefully,\n redirect the user to `/blog/first-post/`.\n\n- If redirect map generation is enabled, it will include an entry\n mapping `/blog/test-post` to `/blog/first-post/`.\n\n### Configuration File\n\nThe plugin's configuration file is `configs/redirect.ini`.\nSettings should be in a `[redirect]` section.\nThere are currently three configurable settings. Here is an example:\n\n```ini\n[redirect]\n# The name of the field from which redirects are extracted.\n# The default value is \"redirect_from\"\nredirect_from_field = redirect_from\n\n# Set template used to render redirect pages.\n# There is no default value \u2014 if no template is set, redirect\n# page generation is disabled.\ntemplate = redirect.html\n\n# Set the name of the redirect map file.\n# There is no default value \u2014 if no value is set, redirect\n# map generation is disabled.\nmap_file = .redirect.map\n```\n\n### Redirect Pages\n\nIf a `template` is configured in the plugin configuration file (`configs/redirect.ini`),\n_redirect pages_ will be generated from the specified template. The intention is that\nthe resulting page will redirect the user to the target location using [meta refresh]\nand/or a [javascript redirect].\n\nWithin the template, the target of the redirect is available as `this.target`.\n\nAn simple example for such a template is:\n\n```jinja\n<!doctype html>\n<html>\n <head>\n <title>Page Moved</title>\n <link rel=\"canonical\" href=\"{{ this.target|url(external=true) }}\">\n\n <!-- meta refresh redirect -->\n <meta http-equiv=\"refresh\" content=\"0; url={{ this.target|url(absolute=true) }}\">\n\n <!-- javascript redirect -->\n <script type=\"text/javascript\">\n window.location.href = {{ this.target|url(absolute=true)|tojson }};\n </script>\n </head>\n <body>\n <h1>Page Moved</h1>\n <p>\n If you are not automatically redirected, the page you want can be found at\n <a href=\"{{ this.target|url }}\">{{ this.target|url(external=true) }}</a>.\n </p>\n </body>\n</html>\n```\n\n> [!TIP]\n> For the `url(external=true)` and `url(absolute=true)`\n filters to work, a `[url][project config]` may need to be configured\n for the project.\n\nWhen redirecting from URLs that do not end with `.html` or `.htm`, the redirect page\nis generated at the url with `/index.html` appended.\nFor example if there is a redirect from `/old-image.png` to\n`/new-image.png`, the redirect page will be generated at\n`/old-image.png/index.html`.\nThis is done with the hope that the web server, without extra\nconfiguration, will respond to a request for `/old-image.png` with a\ncontent-type header of `text/html`.\n\n### Redirect Map\n\nIf a `map_file` is configured in the plugin configuration file (`configs/redirect.ini`),\na *map file* will be generated in the output tree.\n\nThe map file is in a format suitable for inclusion in an *nginx* [map block][nginx map].\nAssuming there is a single redirect from `/old-page` to `/new-page`, the contents\nof the map file would be:\n\n```\n/old-page/ /replacement-page/;\n```\n\nAssuming that `map_file` is set to `.redirect.map`, the salient parts\nof an *nginx* configuration file that utilizes the redirect map might\nlook like:\n\n```nginx\n[...]\n\nhttp {\n [...]\n\n # You may need to adjust this (and/or map_hash_max_size) to avoid\n # \"could not build map_hash, you should increase map_hash_bucket_size\"\n # error from nginx\n map_hash_bucket_size 128;\n\n map $uri $redirect_to_uri {\n default \"\";\n include /path/to/htdocs/.redirect.map;\n }\n\n server {\n listen [...];\n [...];\n\n root /path/to/htdocs;\n\n location ~ /\\. {\n # Don't serve dot-files (like .redirect.map)\n return 404;\n }\n\n if ($redirect_to_uri) {\n # pass query args to preserve utm_* tracking parameters, etc.\n return 301 $redirect_to_uri$is_args$args;\n }\n\n [...]\n }\n}\n```\n\n## To Do\n\n- Make this work for Lektor projects with [alternatives] enabled.\n\n- Add support for writing the redirect map file in other formats.\n (E.g. Apache [text map][apache text map] format.)\n\n[alternatives]: https://www.getlektor.com/docs/content/alts/\n[meta refresh]: https://developers.google.com/search/docs/crawling-indexing/301-redirects#metarefresh\n[javascript redirect]: https://developers.google.com/search/docs/crawling-indexing/301-redirects#jslocation\n[project config]: https://www.getlektor.com/docs/project/file/#project\n[nginx map]: https://nginx.org/en/docs/http/ngx_http_map_module.html\n[field config]: https://www.getlektor.com/docs/models/#fields\n[apache text map]: https://httpd.apache.org/docs/current/rewrite/rewritemap.html#txt\n\n## Author\n\nJeff Dairiki <dairiki@dairiki.org>\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A Lektor plugin to help with generating redirects for, e.g., moved pages.",
"version": "0.1.0b2",
"project_urls": {
"Home": "https://github.com/dairiki/lektor-redirect"
},
"split_keywords": [
"lektor",
" plugin"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "2eb2dca493a89073d635f4ca34bbaf643825a3eb6e38acdc15b55be353cac049",
"md5": "ffd8bfcfb811e8ce395df7193115a94b",
"sha256": "b77e0c25fa81b5e72d13df2dde70f6cf117705b9bfad527aeb757a486d0abee6"
},
"downloads": -1,
"filename": "lektor_redirect-0.1.0b2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ffd8bfcfb811e8ce395df7193115a94b",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 11657,
"upload_time": "2024-08-18T17:39:11",
"upload_time_iso_8601": "2024-08-18T17:39:11.725291Z",
"url": "https://files.pythonhosted.org/packages/2e/b2/dca493a89073d635f4ca34bbaf643825a3eb6e38acdc15b55be353cac049/lektor_redirect-0.1.0b2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "5e16264743974b3b38d1d322672c44279141e78a65d16cdd0ca6fbbc82108c59",
"md5": "33bda4a4a59bfa43874bc1e48d26f3f8",
"sha256": "38fab44bda6170845f542f24a1d793ce6fac550bdf26bff260cdcbf5b27ae461"
},
"downloads": -1,
"filename": "lektor_redirect-0.1.0b2.tar.gz",
"has_sig": false,
"md5_digest": "33bda4a4a59bfa43874bc1e48d26f3f8",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 49579,
"upload_time": "2024-08-18T17:39:13",
"upload_time_iso_8601": "2024-08-18T17:39:13.382546Z",
"url": "https://files.pythonhosted.org/packages/5e/16/264743974b3b38d1d322672c44279141e78a65d16cdd0ca6fbbc82108c59/lektor_redirect-0.1.0b2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-08-18 17:39:13",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "dairiki",
"github_project": "lektor-redirect",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "lektor-redirect"
}