Check out a free preview of the full Full Stack for Front-Ends Part 2 course:
The "Redirect" Lesson is part of the full, Full Stack for Front-Ends Part 2 course featured in this preview video. Here's what you'd learn in this lesson:

After describing how to redirect on your website to anywhere on the web, Jem discusses the difference between 301 and 302 redirects.

Get Unlimited Access Now

Transcript from the "Redirect" Lesson

[00:00:00]
>> Jem Young: So now we can add in a redirect block if you want. This is optional because we don't have anywhere to redirect to. This is just more fyi, this is actually how my side is serving up these slides. If you go to jemyoung.com/frontendfullstack, how it gets redirected to Google slides.

[00:00:15] I'm just doing this in a location block on my server. So I'm saying the final location, such as frontendfullstack2, and this return 301 to a different URL. That's it, that's how you do redirects in Nginx. What's the difference between 301 and 302 for responses?
>> Speaker 2: One is permanent

[00:00:36]
>> Jem Young: Yes, exactly one is permanent and one is temporary, when its permanent as in 301. Google will cache that or most web browsers and most search engines will cache that. So it doesn't even have to look that up anymore. It doesn't have to hit your server, get a 301 response.

[00:00:50] It has that in its cache. 302 just means move temporarily. So just for now we're gonna keep hitting the server or we're gonna honor this redirect header temporarily. So that's the difference between 301 and 302. You can get into trouble if you 301 a redirect and then you wanna fix that later because it's already cached.

[00:01:08] So be careful with your redirects. But in general, I did it one more time. In general, if you wanna redirect, it's pretty straightforward in Nginx.
>> Speaker 3: Can you use a regular expression instead of plain name, filename.
>> Jem Young: Yes. Nice job, you're a nail on the question that you can actually use the regular expression with location block.

[00:01:31] So if you wanna say I only wanna redirect people that are looking for slash help and JPEG, so start at JPEG, we can do that but not PNGs. If you wanna get [INAUDIBLE], but it takes right of your expression just like [INAUDIBLE]. But good question cuz if you're getting two fine grains Nginx streaming, you probably [INAUDIBLE] regular expressions for path matching and things like that.

[00:01:54] But yeah, so if you wanna add redirects, that's how you do it in your website. And we're not just gonna do it because it's pretty straightforward