Posted almost 5 years ago. Visible to the public. Repeats.

Be very careful with 301 and 308 redirects

Browsers support different types of redirects.

Be very careful with these status codes:

  • 301 Moved Permanently
  • 308 Permanent Redirect

Most browsers seem to cache these redirects forever, unless you set different Cache-Control headers. If you don't have any cache control headers, you can never change them without forcing users to empty their cache.

Note

By default Archive Rails sends a header Cache-Control: max-age=0, private, must-revalidate with all responses, including redirects. That means redirects are never cached by browsers.

You do need to pay attention if you redirect outside of Rails, e.g. via your web server configuration.

Dealing with incorrectly cached redirects

The only fix is to keep redirecting the user to the correct page, so if you had

Copy
flowchart LR /page1--301-->/page2

but you want

Copy
flowchart LR /page1--301-->/page3

your only fix is to change it to

Copy
flowchart LR /page1--301-->/page2 /page2--301-->/page3

This means that /page2 will become unusable as its own page.

Note that a 301 with an explicit expiry via Cache-Control is fine, and might be preferred for SEO purposes.

Your development team has a full backlog of feature requests, chores and refactoring coupled with deadlines? We are familiar with that. With our "DevOps as a Service" offering, we support developer teams with infrastructure and operations expertise.

Owner of this card:

Avatar
Tobias Kraze
Last edit:
3 months ago
by Henning Koch
About this deck:
We are makandra and do test-driven, agile Ruby on Rails software development.
License for source code
Posted by Tobias Kraze to makandra dev
This website uses short-lived cookies to improve usability.
Accept or learn more