Page 1 of 1

How to redirect trailing slash URLs?

Posted: Sun Nov 05, 2017 6:13 pm
by StevieWonderer
My routing.yml is this:

Code: Select all

vendorname_extensionname_a:
    path: /a
    defaults: { _controller: vendorname.extensionname.a:default }
Now, if a user requests /a/, he gets "No route found for "GET /a/"".

This is unacceptable so how should I do a redirect to "/a"?

Re: How to redirect trailing slash URLs?

Posted: Mon Nov 06, 2017 3:00 pm
by rxu
You would need to do something like that:

Code: Select all

vendorname_extensionname_a:
    path: /a{trailing}
    defaults: { _controller: vendorname.extensionname.a:default, trailing: '' }
    requirements:
        trailing: '/?'

Re: How to redirect trailing slash URLs?

Posted: Mon Nov 06, 2017 3:26 pm
by StevieWonderer
Then googlebot can find duplicate content in /a and /a/ and give me a penalty :( That's why I'd like to do a redirect.

Re: How to redirect trailing slash URLs?

Posted: Mon Nov 06, 2017 4:58 pm
by rxu
The thing I posted above will make both links with and without the slash work.
304 redirect (or canonical link or anything like that to avoid duplicating content) is another thing you have to do yourself.
I just answered the question you asked :)

Re: How to redirect trailing slash URLs?

Posted: Mon Nov 06, 2017 6:19 pm
by StevieWonderer
rxu wrote:
Mon Nov 06, 2017 4:58 pm
The thing I posted above will make both links with and without the slash work.
304 redirect (or canonical link or anything like that to avoid duplicating content) is another thing you have to do yourself.
I just answered the question you asked :)
Thank you for your answer. My question may have been confusing but anyway, by "redirect" I meant "http redirect".

I think I found a solution though. When I was writing this reply, I realized the following works:

Code: Select all

$response = new \Symfony\Component\HttpFoundation\RedirectResponse($this->helper->route('vendorname_extensionname_a'), 301);
$response->send();
I'm 97% sure it did not work properly yesterday because no-cache headers were not included. But because of a reason unknown to me, the proper headers are there now. :geek: