302 vs 301 - controller redirect method should use 301 by default?

50 views
Skip to first unread message

Nicolaas Thiemen Francken - Sunny Side Up

unread,
Jun 19, 2015, 5:35:45 AM6/19/15
to silverstripe-dev
The same client also pointed out the issues with trailing slash vs no trailing slash also asked me wht the redirect method uses 302  (temporary redirect) rather than 301 (permanent redirect) by default, since the latter are way more common. 

I have not given it much thought, but perhaps someone can explain the history of the "302 default" and how that is supposed to work, or what I am missing here ;-)

Thank you

Nicolaas

Sam Minnée

unread,
Jun 19, 2015, 7:31:28 AM6/19/15
to silverst...@googlegroups.com, n...@sunnysideup.co.nz, n...@sunnysideup.co.nz
Temporary redirects are what you should use after a form submission, so I would challenge the assertion that 301 redirects are more common in terms of the amount of code you write. Maybe in terms of web requests there are a lot of 301 redirects, but most of these are likely handled by the same piece of code: RedirectorPage.

Temporary redirects are safer. If you use a temporary redirect where you should use a permanent, you get sub-optimal SEO; if you use a permanent redirect when you should use a temporary you get hard-to-diagnose bugs.

It's really easy to do a 301 redirect — $this->redirect($link, 301) — and if the issue is that developers don't remember to think about this, well, the default option should be safe before it's optimised.

So, 302 is the best option to use as a default.

Finally, even if 301 was the best option to use as a default, changing it now would break a bunch of existing code, so there would need to be a really good reason for changing.

Daniel Hensby

unread,
Jun 20, 2015, 3:27:34 AM6/20/15
to silverst...@googlegroups.com, n...@sunnysideup.co.nz

The difference between them has significant meaning to browsers and bots.

What a 301 is saying is "if you ever come to this url again, you should go to this other one ALWAYS".

A 302 is effectively saying "just this time go to this page" so next time you need to check again and I might send you somewhere else.

What does this mean in reality?

301s are used for pages that are moved or renamed, or for common redirects like forcing www on a URL. You're telling a bit and Google "that url is out of date, it's now located somewhere else".

A 302 is typically used for form submissions or after performing a successful action that requires user input and where the location night change depending on that input or user state. If a user is not logged in, for example, and they try to go to /admin, if you 301 them to the login page, the next time they try to go to /admin (even if they are authenticated) the browser will send them to login again.

It sounds like your SEO agency needs a basic lesson in HTTP response codes...


--
You received this message because you are subscribed to the Google Groups "SilverStripe Core Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to silverstripe-d...@googlegroups.com.
To post to this group, send email to silverst...@googlegroups.com.
Visit this group at http://groups.google.com/group/silverstripe-dev.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages