Default to performing a 301 redirect for URIs with trailing slashes

This commit is contained in:
Andy Miller
2015-08-31 21:28:00 -06:00
parent b9e24712a8
commit dd00f34cb8
3 changed files with 18 additions and 1 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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);