1 00:00:04,300 --> 00:00:10,620 Now that we have an understanding of engine X variables we can move on to rewrites or simply redirect. 2 00:00:10,630 --> 00:00:17,320 In some cases the two directives we can use for rewriting requests is the rewrite directive and the 3 00:00:17,320 --> 00:00:23,060 return directive which we've already used several times throughout this course and being the same bluff. 4 00:00:23,080 --> 00:00:30,220 The two very importantly however the return statement as we've been using it takes a status code and 5 00:00:30,220 --> 00:00:33,040 response data or strings. 6 00:00:33,040 --> 00:00:40,630 But in the case of that response code being a 300 variant which is four redirects the return directives 7 00:00:40,630 --> 00:00:45,650 behaviour changes in it then except a you are dry as the second parameter. 8 00:00:45,970 --> 00:00:46,650 A You are right. 9 00:00:46,660 --> 00:00:49,360 To which the client should be redirected. 10 00:00:49,360 --> 00:00:56,700 For example at the moment we can access the demo sites image by requesting thumb dot P in G. 11 00:00:57,100 --> 00:01:04,600 But let's say we also want to serve this image for Slash logo which obviously right now returns a foro 12 00:01:04,600 --> 00:01:09,620 for we can create a simple redirect using the return directive. 13 00:01:09,930 --> 00:01:12,900 I'll get rid of all these bits from the previous lesson. 14 00:01:12,970 --> 00:01:15,600 Change this location to that slash logo. 15 00:01:15,610 --> 00:01:16,660 You are right. 16 00:01:16,960 --> 00:01:25,500 And in this location return a 3 0 7 so a temporary redirect again or 300 status codes being redirects 17 00:01:25,500 --> 00:01:26,830 of some sort. 18 00:01:26,950 --> 00:01:30,060 And what we want to return is slash thumb. 19 00:01:30,070 --> 00:01:31,720 Dot P in G. 20 00:01:31,720 --> 00:01:33,370 We could also have made this a complete. 21 00:01:33,370 --> 00:01:36,150 You are right with protocol host etc.. 22 00:01:36,370 --> 00:01:41,830 But a relative you are I will be fine as this is redirecting to the same host as the request this coming 23 00:01:41,830 --> 00:01:50,380 from save this reload the configuration and now when we reload this request for Slash logo we get the 24 00:01:50,380 --> 00:01:52,280 thumb doppie G image. 25 00:01:52,570 --> 00:01:58,050 But very importantly note that the u r l changed to thumb don't be in G. 26 00:01:58,210 --> 00:02:04,270 Which is essentially the main difference between rewrites and redirects a redirect simply tells the 27 00:02:04,270 --> 00:02:05,730 client performing the request. 28 00:02:05,740 --> 00:02:08,610 Where to go instead a rewrite. 29 00:02:08,620 --> 00:02:12,240 On the other hand mutates the you are right internally. 30 00:02:12,310 --> 00:02:18,730 So let's see an example this time in the server context before matching the request to any location 31 00:02:18,730 --> 00:02:19,910 contexts. 32 00:02:20,050 --> 00:02:22,070 We can say rewrite. 33 00:02:22,240 --> 00:02:26,280 Pass a regular expression with which to match the you are I. 34 00:02:26,500 --> 00:02:34,540 I'll say Carrott So starting with Slash user slash and then W. plus. 35 00:02:34,750 --> 00:02:42,100 So more than one word character with the second argument being the new you are try to rewrite this to 36 00:02:42,550 --> 00:02:44,540 slash greet. 37 00:02:44,550 --> 00:02:47,940 Now here's the important thing to understand about rewrites. 38 00:02:48,190 --> 00:02:55,630 When you are IE is rewritten it also gets re-evaluated by engine X as a completely new request meaning 39 00:02:55,690 --> 00:02:57,280 at this point here. 40 00:02:57,520 --> 00:03:04,020 The rewritten greet you are I start to right from the top again and will get re-evaluated again. 41 00:03:04,210 --> 00:03:11,380 The re-evaluation makes rewrites very powerful but also requires more system resources than say a return. 42 00:03:11,380 --> 00:03:14,680 Just something to keep in mind when creating rewrite rules. 43 00:03:14,890 --> 00:03:21,700 Okay once rewritten to slash greet this request will move past this rewrite so let's create a location 44 00:03:21,700 --> 00:03:23,170 to match add on. 45 00:03:23,170 --> 00:03:34,030 I'll change this one and return a simple 200 worth Hello user save reload the configuration. 46 00:03:34,030 --> 00:03:36,460 Keeping in mind what our rewrite checks for. 47 00:03:36,460 --> 00:03:43,210 I'll request slash user slash the first part of that rewrite rule John. 48 00:03:43,240 --> 00:03:45,700 The second part with the word match. 49 00:03:45,940 --> 00:03:46,670 Enter. 50 00:03:46,960 --> 00:03:48,520 Hello user. 51 00:03:48,520 --> 00:03:55,570 So clearly the response from slash greet although the browser is still pointing to slash user slash 52 00:03:55,570 --> 00:04:02,290 John meaning the request was rewritten internally only unbeknownst to the client. 53 00:04:02,290 --> 00:04:08,130 Another important and powerful feature of rewrites is the ability to capture certain parts of the original. 54 00:04:08,130 --> 00:04:09,010 You are right. 55 00:04:09,340 --> 00:04:15,840 Using standard regular expression capture groups or example let's say we want to capture a user name. 56 00:04:15,940 --> 00:04:22,870 This word character section of the regular expression simply wrap that part in braces like so. 57 00:04:23,110 --> 00:04:29,320 Which being the first capshaw group in this you are they can be accessed as dollar signs so a variable 58 00:04:29,500 --> 00:04:32,960 one should we have had a second caption Ruby here. 59 00:04:32,980 --> 00:04:38,990 Like this we could have access that value as dollar sign too and so on. 60 00:04:38,990 --> 00:04:46,000 OK so with this variant we're now requesting slash user slash John as we did in the browser and then 61 00:04:46,000 --> 00:04:52,570 rewriting that to slash greet slash John as that will be the value of this capture group. 62 00:04:52,570 --> 00:04:58,990 We can test this by creating an exact match location specifically for Slash greet slash John meaning 63 00:04:59,050 --> 00:05:03,600 any other user will be matched on the standard prefix match and only John. 64 00:05:03,710 --> 00:05:08,860 This exact match returned 200 Hello John. 65 00:05:09,800 --> 00:05:11,730 Reload these changes. 66 00:05:11,730 --> 00:05:18,980 And now what should happen when I refresh this is that slash user slash John should get rewritten to 67 00:05:18,980 --> 00:05:27,320 slash greet slash John revaluated as that new you are II so skipping past the rewrite and then even 68 00:05:27,320 --> 00:05:33,620 though this slash Greek location is a match I was slash greets slash John exact match will take priority 69 00:05:33,650 --> 00:05:34,320 and return. 70 00:05:34,340 --> 00:05:35,370 Hello John. 71 00:05:36,080 --> 00:05:37,020 Test it. 72 00:05:37,780 --> 00:05:39,300 And it works. 73 00:05:39,310 --> 00:05:40,800 Change this to any other user. 74 00:05:40,810 --> 00:05:41,780 However. 75 00:05:41,800 --> 00:05:42,640 Jane. 76 00:05:43,400 --> 00:05:46,110 And we get the prefix match with Hello user. 77 00:05:47,390 --> 00:05:48,800 Get the last part of rewrites. 78 00:05:48,810 --> 00:05:55,860 I'm going to cover is passing optional flags or one in particular the last flag to demonstrate. 79 00:05:55,880 --> 00:05:57,750 OK read another rewrite here. 80 00:05:57,840 --> 00:06:04,550 This time matching slash greet slash John and rewriting it to thumb dot B in G. 81 00:06:04,590 --> 00:06:06,220 The demo site image. 82 00:06:06,540 --> 00:06:13,260 Meaning that if we now request slash user slash John this first rewrite will matchett rewrite it to 83 00:06:13,260 --> 00:06:20,010 slash graetz lashed John and then have it re-evaluate it where it will then match the second rewrite 84 00:06:20,070 --> 00:06:28,350 and again be rewritten to thumb doppie in G and finally served as thumb dont be in G save and test this. 85 00:06:30,410 --> 00:06:32,170 Request John again. 86 00:06:33,930 --> 00:06:39,140 And sure enough we get the thumb image under this user slash John request. 87 00:06:39,160 --> 00:06:44,670 Now what the last flag allows us to do is essentially tell engine X not to allow you are right to be 88 00:06:44,670 --> 00:06:49,510 rewritten any more on this first rewrite add lost. 89 00:06:49,560 --> 00:06:54,960 So still re-evaluate the new you are right after the rewrite but make sure this is the last time it 90 00:06:54,960 --> 00:07:03,140 gets rewritten meaning it should skip over the second rewrite save reload request that same you are 91 00:07:03,140 --> 00:07:06,560 row with a reload and we're back to hello John. 92 00:07:07,680 --> 00:07:10,170 That wraps up redirects and rewrites. 93 00:07:10,200 --> 00:07:16,700 Although technically there's a third kind of rewrite called Try files which will cover in the next lesson.