Remove trailing slash if it exists in a request

Fixes #36
This commit is contained in:
Dmitriy Simushev 2014-07-14 11:02:20 +00:00
parent 1a3062f2f9
commit 1a60aeac2e
2 changed files with 51 additions and 0 deletions

View File

@ -673,3 +673,13 @@ users_update:
defaults:
_controller: Mibew\Controller\UsersController::updateAction
_access_check: Mibew\AccessControl\Check\LoggedInCheck
# Remove trailing slashe. This route is the last one because previous rotes can
# (but definitely should not) have trailing slashes.
remove_trailing_slash:
path: /{url}
defaults:
_controller: Mibew\Controller\RedirectController::removeTrailingSlashAction
requirements:
url: .*/$
methods: [GET]

View File

@ -0,0 +1,41 @@
<?php
/*
* Copyright 2005-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
namespace Mibew\Controller;
use Symfony\Component\HttpFoundation\Request;
/**
* Process all system level redirect actions.
*/
class RedirectController extends AbstractController
{
/**
* Redirect a user to the URL without trailing slash with 301 HTTP code.
*
* @param Request $request Incoming request
* @return \Symfony\Component\HttpFoundation\RedirectResponse
*/
public function removeTrailingSlashAction(Request $request)
{
$path_info = $request->getPathInfo();
$request_uri = $request->getRequestUri();
$url = str_replace($path_info, rtrim($path_info, ' /'), $request_uri);
return $this->redirect($url, 301);
}
}