diff --git a/system/blueprints/config/system.yaml b/system/blueprints/config/system.yaml index feb0b039a..b7f9e683d 100644 --- a/system/blueprints/config/system.yaml +++ b/system/blueprints/config/system.yaml @@ -139,6 +139,17 @@ form: validate: type: bool + pages.redirect_trailing_slash: + type: toggle + label: PLUGIN_ADMIN.REDIRECT_TRAILING_SLASH + help: PLUGIN_ADMIN.REDIRECT_TRAILING_SLASH_HELP + highlight: 1 + options: + 1: PLUGIN_ADMIN.YES + 0: PLUGIN_ADMIN.NO + validate: + type: bool + languages: type: section title: PLUGIN_ADMIN.LANGUAGES diff --git a/system/config/system.yaml b/system/config/system.yaml index 77edb1d93..9b3bbc4dd 100644 --- a/system/config/system.yaml +++ b/system/config/system.yaml @@ -46,6 +46,7 @@ pages: etag: false # Set the etag header tag vary_accept_encoding: false # Add `Vary: Accept-Encoding` header redirect_default_route: false # Automatically redirect to a page's default route + redirect_trailing_slash: true # Handle automatically or 301 redirect a trailing / URL cache: enabled: true # Set to true to enable caching diff --git a/system/src/Grav/Common/Uri.php b/system/src/Grav/Common/Uri.php index 7f8275d45..070807ba5 100644 --- a/system/src/Grav/Common/Uri.php +++ b/system/src/Grav/Common/Uri.php @@ -31,7 +31,6 @@ class Uri */ public function __construct() { - $base = 'http://'; $name = isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost'; $port = isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : 80; @@ -80,6 +79,11 @@ class Uri $config = $grav['config']; $language = $grav['language']; + // If configured to, redirect trailing slash URI's with a 301 redirect + if ($config->get('system.pages.redirect_trailing_slash', false) && $this->url != '/' && Utils::endsWith($this->url, '/')) { + $grav->redirect(rtrim($this->url, '/'), 301); + } + // resets $this->paths = []; $this->params = []; @@ -105,6 +109,7 @@ class Uri } } + // split the URL and params $bits = parse_url($uri);