项目作者: bnomei

项目描述 :
Setup HTTP Status Code Redirects from within the Kirby Panel
高级语言: PHP
项目地址: git://github.com/bnomei/kirby3-redirects.git
创建时间: 2018-10-09T15:56:09Z
项目社区:https://github.com/bnomei/kirby3-redirects

开源协议:MIT License

下载


Kirby Redirects

Kirby 5
PHP 8.2
Release
Downloads
Coverage
Maintainability
Discord
Buymecoffee

Features

  • ✏️ Define HTTP Status Code redirects from within the Kirby Panel.
  • 🔀 Setup redirects from any request URI to any response URI, not just Kirby routes.
  • 🕵️ Match on query-strings like ?foo=bar and forward data with regex (?P<year>\d{4}).
  • 🛡️ Protects your website from attacks by blocking 50+ routes/patterns of other popular CMS.
  • 🪝 With the hooks you can add custom logic like logging 404s.
  • 🏎️ Highly performant due to caching on repeated valid requests.

Installation

  • unzip master.zip as folder
    site/plugins/kirby3-redirects or
  • git submodule add https://github.com/bnomei/kirby3-redirects.git site/plugins/kirby3-redirects or
  • composer require bnomei/kirby3-redirects

Setup: add Redirects section to Site blueprint

Add the plugin-redirects section to your site.yml. This will allow you to create redirects in via the Panel or
programmatically.

site/blueprints/site.yml

  1. sections:
  2. # ...other sections
  3. redirects:
  4. extends: plugin-redirects3xx

[!TIP]
If you want to able to set all HTTP Status code from within the panel, not just the 3xx range, you can use
extends: plugin-redirects instead.

[!TIP]
Instead of using the site blueprint you can also use the redirects section in any pages blueprint as long as you
adjust the bnomei.redirects.map option accordingly so the plugin knows where to find the redirects.

Usage

In the Structure Field within the Panel add a Request-URIs fromuri, set a Response-URIs touri and a HTTP Status Code code like 301 or 302.

fromuri to code
projects/cool projects/changed-slug 301
projects/cool.html projects/changed-slug 301
projects\/.*\.html projects/changed-slug 301
some/broken-link https://exter.nal 301
blog\/(?P<year>\d{4})_(?P<slug>.*)\.html blog/$year/$slug 301

Shielding your website from attacks

This plugin will block 50+ routes/patterns of other popular CMS. It is enabled by default and will reduce the load
on your website caused by bots/attackers looking for vulnerabilities found in other CMS.

  • Drupal
  • Joomla
  • Magento
  • Shopify
  • Wordpress

You can track any redirects, including the blocked requests from the shield, using the redirect:before and
404:before hooks.

Hooks

This plugin will trigger the following hooks, which you could use to build your own tracking or logging.

  • redirect:before($code, $redirect)
  • 404:before($route, $path, $method)

site/config/config.php

  1. <?php
  2. return [
  3. 'hooks' => [
  4. 'redirect:before' => function (int $code, \Bnomei\Redirect $redirect) {
  5. // do whatever you need, like...
  6. monolog('redirect')->info($code, [
  7. 'from' => $redirect->from(),
  8. 'to' => $redirect->to()
  9. ]);
  10. },
  11. '404:before' => function (\Kirby\Http\Route $route, string $path, string $method) {
  12. // do whatever you need, like...
  13. F::write(kirby()->root('logs').'/404.log', implode(' ', [
  14. '['.date('Y-m-d H:i:s').']',
  15. '404.ERROR',
  16. $method,
  17. $path,
  18. PHP_EOL,
  19. ]), true);
  20. },
  21. ],
  22. // other config...
  23. ];

Programmatically changing the redirects table

The site methods appendRedirects and removeRedirects allow you to programmatically change the redirects table (if
stored in a Page/Site-Object Field, see map config option).

  1. // add single item
  2. $success = site()->appendRedirects(
  3. ['fromuri' => '/posts?id=1', 'touri' => '/blog/1', 'code' => 301]
  4. );
  5. // add multiple items with nested array
  6. $success = site()->appendRedirects([
  7. ['fromuri' => '/posts?id=2', 'touri' => '/blog/2', 'code' => 301],
  8. // ...
  9. ['fromuri' => '/posts?id=999', 'touri' => '/blog/999', 'code' => 301],
  10. ]);
  11. // remove single item
  12. $success = site()->removeRedirects(
  13. ['fromuri' => '/posts?id=1', 'touri' => '/blog/1']
  14. );
  15. // remove multiple items with nested array
  16. $success = site()->removeRedirects([
  17. ['fromuri' => '/posts?id=3', 'touri' => '/blog/3'],
  18. ['fromuri' => '/posts?id=5', 'touri' => '/blog/5'],
  19. ['fromuri' => '/posts?id=7', 'touri' => '/blog/7'],
  20. ]);

Cache & Performance

The plugin will cache any valid URI request and thus vastly improve performance on repeated requests to that URI. Thus
the plugin will not check for redirects at all if it know that the URI will lead to a valid content page in Kirby. When
the redirects table is changed or any content is updated via the Panel the cache will be cleared.

For best performance, set either
the global or plugin-specific cache driver to one using the
server’s memory, not the default using files on the hard disk (even on SSDs). If available, I suggest Redis/APCu or
leave it at file otherwise.

site/config/config.php

  1. return [
  2. 'cache' => [
  3. 'driver' => 'apcu', // or redis
  4. ],
  5. 'bnomei.redirects.cache' => [
  6. 'type' => 'apcu', // or redis
  7. ],
  8. ];

Similar Plugins

  • kirby-retour while featuring a nice UI and built-in 404 tracking it
    can only handle Kirby routes (with pattern matching) but not any request URI.

Settings

bnomei.redirects. Default Description
code 301
querystring true do keep querystring in request URI. example: https://kirby3-plugins.bnomei.com/projects?id=12 => projects?id=12
only-empty-results false only redirect if the result is empty in the router
map callback A closure to get the structure from content/site.txt. Define you own if you want the section to be in a different blueprint or skip the blueprint and just use code.
shield.enabled true Block various routes of other popular CMS

Disclaimer

This plugin is provided “as is” with no guarantee. Use it at your own risk and always test it yourself before using it
in a production environment. If you find any issues,
please create a new issue.

License

MIT

It is discouraged to use this plugin in any project that promotes racism, sexism, homophobia, animal abuse, violence or
any other form of hate speech.