From 4b4569b1b229ae06a0d1eb57a114a4d6fa7632f6 Mon Sep 17 00:00:00 2001 From: Toby Mackenzie Date: Tue, 1 Mar 2016 22:33:12 -0500 Subject: [PATCH 1/3] Remove trailing slash: check for match A 301 seems to imply that the redirect target exists. Redirecting to a 404 page is all the same to a normal end user, but it seems Google will see it as a crawl error. This change just makes sure that there is a route that will match the redirected URL; otherwise a 404 is thrown. --- cookbook/routing/redirect_trailing_slash.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/cookbook/routing/redirect_trailing_slash.rst b/cookbook/routing/redirect_trailing_slash.rst index 1f2f5f89614..fd16b3935d8 100644 --- a/cookbook/routing/redirect_trailing_slash.rst +++ b/cookbook/routing/redirect_trailing_slash.rst @@ -17,6 +17,7 @@ new URL with a 301 response status code:: use Symfony\Bundle\FrameworkBundle\Controller\Controller; use Symfony\Component\HttpFoundation\Request; + use Symfony\Component\Routing\Exception\ResourceNotFoundException; class RedirectingController extends Controller { @@ -27,6 +28,11 @@ new URL with a 301 response status code:: $url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri); + try{ + $this->get('router')->match(str_replace('/app_dev.php', '', $url)) + }catch(ResourceNotFoundException $e){ + throw $this->createNotFoundException(); + } return $this->redirect($url, 301); } } From 262b20ac01b98a8b1bff341cef3e04ba11ccd227 Mon Sep 17 00:00:00 2001 From: Toby Mackenzie Date: Thu, 6 Jul 2017 21:49:00 -0400 Subject: [PATCH 2/3] whitespace --- cookbook/routing/redirect_trailing_slash.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cookbook/routing/redirect_trailing_slash.rst b/cookbook/routing/redirect_trailing_slash.rst index fd16b3935d8..31eec67c29c 100644 --- a/cookbook/routing/redirect_trailing_slash.rst +++ b/cookbook/routing/redirect_trailing_slash.rst @@ -29,9 +29,9 @@ new URL with a 301 response status code:: $url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri); try{ - $this->get('router')->match(str_replace('/app_dev.php', '', $url)) + $this->get('router')->match(str_replace('/app_dev.php', '', $url)) }catch(ResourceNotFoundException $e){ - throw $this->createNotFoundException(); + throw $this->createNotFoundException(); } return $this->redirect($url, 301); } From 60df47b4e49bba266880da800baec95f8edf8431 Mon Sep 17 00:00:00 2001 From: Toby Mackenzie Date: Thu, 6 Jul 2017 22:04:00 -0400 Subject: [PATCH 3/3] comment explaining not found exception check --- cookbook/routing/redirect_trailing_slash.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cookbook/routing/redirect_trailing_slash.rst b/cookbook/routing/redirect_trailing_slash.rst index 31eec67c29c..13803f477f8 100644 --- a/cookbook/routing/redirect_trailing_slash.rst +++ b/cookbook/routing/redirect_trailing_slash.rst @@ -28,6 +28,8 @@ new URL with a 301 response status code:: $url = str_replace($pathInfo, rtrim($pathInfo, ' /'), $requestUri); + // send 404 if page doesn't exist. Google sees a 301 as a link and + // will mark destination as a not found error try{ $this->get('router')->match(str_replace('/app_dev.php', '', $url)) }catch(ResourceNotFoundException $e){