Full Stack for Front-Ends Part 2 Redirect
This course has been updated! We now recommend you take the Full Stack for Front-End Engineers, v3 course.
Transcript from the "Redirect" Lesson
>> 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
>> 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 very nuanced, but it takes regular expression just like grep does. But good question cuz if you're getting to fine grains Nginx tuning, you probably start using 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