How to redirect trailing slash URLs?

Discussion forum for Extension Writers regarding Extension Development.
Post Reply
StevieWonderer
Registered User
Posts: 14
Joined: Wed Oct 12, 2011 1:06 am

How to redirect trailing slash URLs?

Post by StevieWonderer » Sun Nov 05, 2017 6:13 pm

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"?

rxu
Extensions Development Team
Posts: 2770
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: How to redirect trailing slash URLs?

Post by rxu » Mon Nov 06, 2017 3:00 pm

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: '/?'

StevieWonderer
Registered User
Posts: 14
Joined: Wed Oct 12, 2011 1:06 am

Re: How to redirect trailing slash URLs?

Post by StevieWonderer » Mon Nov 06, 2017 3:26 pm

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.

rxu
Extensions Development Team
Posts: 2770
Joined: Wed Oct 25, 2006 12:46 pm
Location: Siberia, Russian Federation
Name: Ruslan
Contact:

Re: How to redirect trailing slash URLs?

Post by rxu » 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 :)

StevieWonderer
Registered User
Posts: 14
Joined: Wed Oct 12, 2011 1:06 am

Re: How to redirect trailing slash URLs?

Post by StevieWonderer » Mon Nov 06, 2017 6:19 pm

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:

Post Reply

Return to “Extension Writers Discussion”

Who is online

Users browsing this forum: No registered users and 4 guests

cron