0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Expressのリダイレクト

Posted at

Expressのリダイレクトでハマった

ExpressのアプリケーションでSEO対策のため、リダイレクト処理を実装する必要があった。
ページのリダイレクトをするためにres.redirect('http://example.com')と書いたら、
ステータスコードが302でリダイレクトされた。

デフォルトのステータスコードが302

Expressでは、res.redirectで指定しないとデフォルトで302になる。
もし、301でリダイレクトしたい場合は、res.redirect(301, 'http://example.com')と指定する必要がある。

気になったこと

他のRailsのredirect_toやLarabelのRoute::redirectでもデフォルトが302でリダイレクトされていた。
自分の感覚では、リダイレクトは301がデフォルトというなんとなくのイメージがあり、確認不足であった。

ちなみに、

  • 301 : 恒久的な転送
  • 302 : 一時的な転送

であるため、それを考えると確かにアプリケーションの中で使うリダイレクト処理は、
一時的な転送が多いと思ったので、納得したという初歩的なメモです。

https://expressjs.com/ja/4x/api.html#res.redirect
https://api.rubyonrails.org/classes/ActionController/Redirecting.html
https://readouble.com/laravel/8.x/ja/routing.html

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?