Redirect ending slash to no slash

SEO issue with urls

In Kirby there is no built in redirect from for example

http://example.com/faq/

to

http://example.com/faq

That might be an SEO issue with duplicate content and splitting the link power between the two. It’s not even good for users, because they don’t know which of the two adresses that are the correct one.

Redirect ending slash

I built a plugin that solved the problem with a redirect from the slash version to the no slash version. It does not redirect when in the panel, because that would make it fail.

<?php
redirect_ending_slash();

function redirect_ending_slash() {
	if( is_ending_slash() && ! is_using_panel() && ! is_home() ) {
		go( get_unslashed_url() );
	}
}

function is_home() {
	if( $_SERVER['REQUEST_URI'] === '/' ) return true;
}

function is_ending_slash() {
	if( substr( $_SERVER['REQUEST_URI'], -1 ) === '/' ) return true;
}

function is_using_panel() {
	if( substr( $_SERVER['REQUEST_URI'], 0, 6 ) === '/panel' ) return true;
}

function get_unslashed_url() {
	return url() . substr( $_SERVER['REQUEST_URI'], 0, -1 );
}

Installation

  1. Create a folder in the plugins folder called redirect-ending-slash
  2. Create the php file redirect-ending-slash.php.
  3. Copy paste the code above into the php file.
  4. Done!

If panel folder has moved

If you move your panel folder around you need to change the plugin a little bit.

Issue with the $page object

Because of the issue below I could not use the $page object to get the url contents. Instead $_SERVER is used. Might be a little bit faster this way.

http://forum.getkirby.com/t/functions-unable-to-load-the-page-object/1081/7

Related but the other way around:

Much better:

https://github.com/jenstornell/slash-redirect