bnomei / kirby3-redirects
Setup performant HTTP Status Code Redirects from within the Kirby Panel
Installs: 7 919
Dependents: 0
Suggesters: 0
Security: 0
Stars: 22
Watchers: 3
Forks: 2
Open Issues: 2
Type:kirby-plugin
Requires
- php: >=8.2
- getkirby/composer-installer: ^1.2
Requires (Dev)
- getkirby/cms: ^5.0.0-alpha.4
- larastan/larastan: ^v3.0.0
- laravel/pint: ^1.13
- pestphp/pest: ^v3.5.1
- spatie/ray: ^1.39
Suggests
- bnomei/kirby3-feed: Add Atom/JSON/RSS feeds or XML-Sitemap to your Kirby site.
- bnomei/kirby3-monolog: Logging for Kirby with Monolog, ideal if you want to start tracking the hooks
- bnomei/kirby3-robots-txt: Automatic robots.txt. Detects and adds XML-Sitemaps from plugins.
- bnomei/kirby3-security-headers: CSP headers to make the the web a saver place. Sensible defaults with zero configuration.
- dev-master
- 5.0.0
- 4.3.0
- 4.2.3
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 1.12.0
- 1.11.0
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.0
- 1.7.0
- 1.6.0
- 1.5.9
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.3
- 1.2.1
- 1.2.0
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.1
- 1.0.0
- dev-dependabot/composer/guzzlehttp/psr7-2.5.0
This package is auto-updated.
Last update: 2024-11-29 14:58:20 UTC
README
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
orcomposer 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
sections: # ...other sections redirects: 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
.
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
<?php return [ 'hooks' => [ 'redirect:before' => function (int $code, \Bnomei\Redirect $redirect) { // do whatever you need, like... monolog('redirect')->info($code, [ 'from' => $redirect->from(), 'to' => $redirect->to() ]); }, '404:before' => function (\Kirby\Http\Route $route, string $path, string $method) { // do whatever you need, like... F::write(kirby()->root('logs').'/404.log', implode(' ', [ '['.date('Y-m-d H:i:s').']', '404.ERROR', $method, $path, PHP_EOL, ]), true); }, ], // other config... ];
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).
// add single item $success = site()->appendRedirects( ['fromuri' => '/posts?id=1', 'touri' => '/blog/1', 'code' => 301] ); // add multiple items with nested array $success = site()->appendRedirects([ ['fromuri' => '/posts?id=2', 'touri' => '/blog/2', 'code' => 301], // ... ['fromuri' => '/posts?id=999', 'touri' => '/blog/999', 'code' => 301], ]); // remove single item $success = site()->removeRedirects( ['fromuri' => '/posts?id=1', 'touri' => '/blog/1'] ); // remove multiple items with nested array $success = site()->removeRedirects([ ['fromuri' => '/posts?id=3', 'touri' => '/blog/3'], ['fromuri' => '/posts?id=5', 'touri' => '/blog/5'], ['fromuri' => '/posts?id=7', 'touri' => '/blog/7'], ]);
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
return [ 'cache' => [ 'driver' => 'apcu', // or redis ], 'bnomei.redirects.cache' => [ 'type' => 'apcu', // or redis ], ];
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
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
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.