Kirby Redirect 301


#1

https://github.com/jenstornell/redirect301

Kirby Redirect 301

Version 0.1

Redirect internal uris to other internal uris with status 301.

Installation

  1. Add redirect301 folder in site/plugins/.
  2. See usage how to add redirects.

Usage

It works like key value pairs, from one uri to another uri:

c::set('plugin.redirect301', [
  'my/old/uri'   => 'my/new/cool/uri/',
  'another/one/' => 'another/new'
]);
  • As you can see you can have an ending / in the uri if you want.
  • The uri does not need to be existing pages.
  • It does not work with full urls in this version, just uris.

Requirements

Kirby 2.3

Why not use htaccess?

If you feel like using htaccess for it I will not stop you but I use it because of this:

  • Kirby core sometimes change the htaccess. I like to keep it clean from custom stuff.
  • With PHP it’s possible to add logic to the rules.

Redirect URL with trailing slash to canonical URL without trailing slash
#2

The plugin machine strikes again! :zap: :yum:


#3

@jenstornell i have a Question!, what does this Plugin?, What is the Function of it?


#4

It’s kind of like this but in PHP:

Redirect pages to another pages. Maybe you have /post and want to redirect it to /blog/post.


#5

so is this just an easier way of using the kirby rooting?
https://getkirby.com/docs/developer-guide/advanced/routing


#6

Yeah. It’s possible to redirect with a plugin, a route, a controller, a template or a snippet. As long as it’s done before the html (or any output on the screen), it works.

In a route it would be like this for a single url:

c::set('routes', array(
  array(
    'pattern' => 'my/awesome/url',
    'action'  => function() {
      header::redirect('http://example.com/something/cool');
    }
  )
));

Might be messy for 100 urls.


#7

Just want to note that redirects are seen as an anti-pattern for performance and should be avoided when ever possible.

So I hope you don’t need to redirect 100 urls :slight_smile:


#8

True, but this might be necessary for old urls which are no longer valid.

If possible, such redirects should be done in the server config though, or in the .htaccess, and only in php as a last resort.


#9

I used your anti-pattern today. :slight_smile: It’s a good way to tell both users and search engines that the urls have changed and where the new page is. I could have left with the old url structure but it was not perfect.

It’s like moving to a new apartment. Then it’s often needed to send post cards to your friends and tell them about the move and new address, even if it’s an anti-pattern. The best would probably stay in the same apartment, but sometimes it’s needed to move on. :wink:

@texnixe Do you say that my awesome plugin should only be used as a last resort? Whatta! :smiley: :wink:

Sometimes I wish there was ìnclude in htaccess to separate custom stuff from core stuff. :slight_smile:


#10

Well, if you have access to the server config file, you can use includes. You would not need the .htaccess file at all, either, because all that can go into the server config.

<VirtualHost ${APACHE_IP}:80>
 ServerName example.com
 ServerAlias www.example.com
 ServerAdmin webmaster@example.com

 ServerSignature Email

 Include /srv/customers/xxxx/apache2/example.redirects

 Use CustomerLogConfig xxxx example.com www

 Use Expires
 Use Deflate

 DocumentRoot "/srv/customers/xxxx/vhosts/example.com/www/htdocs"

 DirectoryIndex index.php

 <Directory "/srv/customers/xxxx/vhosts/example.com/www/htdocs">
  Use DocRootStandard
  Use Kirby
 </Directory>

 SetEnv ServiceLevel PROD
</VirtualHost>

Use Kirby here contains the original rewrite rules from the original Kirby .htaccess.

The included file contains the redirects.


#11

I’m on a cheap (in money) but really good shared hosting. I bet I don’t have this access but good answer because other might need it. :slight_smile:


#12

I implemented a basic plugin for this earlier, with the following additions:

  • Redirects are defined in a redirects page, allowing users to modify them in the panel
  • The route renders a <meta http-equiv="Refresh" ..> HTML page in addition to the 301 header, enabling support for kirby-staticbuilder.

#13

I don’t know if you have seen this? If I wanted the redirects in the panel I would probably try it out.


#14

Sure thing, I even linked to it in the Gist :smiley:
I implemented my own version to 1) support static rendering via Refresh meta tags and 2) not require a function call in my templates (if a route matches a redirect it should occur without having to modify existing code, imo).
The import/export feature is neat but wasn’t relevant in my case.


#15

I’ve tested the plugin and it does nothing…hmm
What you fill into the structure field, how the URLs are formated?
Do you have a screenshot or some hints for me?

S.


#16

Did you try this instruction?

Call the redirecty() function at the top of your header snippet, and you’re all set!


#17

I tried @mogelbrod version because i need the features:

  1. support static rendering via Refresh meta tags and
  2. not require a function call in my templates (if a route matches a redirect it should occur without having to modify existing code, imo).

#18

Hi Svnt!

Using my redirects.php plugin you need to add the blueprint (src/blueprints/redirect.yaml), create a page called redirects which uses said blueprint. You should then be able to add redirects using the structure field. The old URL should be relative to the root and exclude the leading slash. The new URL can be relative or absolute (including domain). Both old and new values can point to pages.

Examples (old - new):

old-page - new-page
random-url/with-slashes/and-a-file.txt - new-file.txt
domain-redirect - https://google.com

#19

Hi @jenstornell, I thought I knew how to “call” a function & this might be an amateur question, but is this how you “call” the redirecty function in the header snippet:

 <?= site->redirecty() ?>

Thanks!


#20

@luukee That plugin doesn’t seem to exist anymore. If you still have it, there should be some info on how to use it in the readme file that accompanies the plugin.

Unless the redirect method is defined as a custom site method, your code will not work. Apart from that, if it was a $site method, you would have to call it with

$site->redirecty();