WEBVTT

00:04.300 --> 00:10.620
Now that we have an understanding of engine X variables we can move on to rewrites or simply redirect.

00:10.630 --> 00:17.320
In some cases the two directives we can use for rewriting requests is the rewrite directive and the

00:17.320 --> 00:23.060
return directive which we've already used several times throughout this course and being the same bluff.

00:23.080 --> 00:30.220
The two very importantly however the return statement as we've been using it takes a status code and

00:30.220 --> 00:33.040
response data or strings.

00:33.040 --> 00:40.630
But in the case of that response code being a 300 variant which is four redirects the return directives

00:40.630 --> 00:45.650
behaviour changes in it then except a you are dry as the second parameter.

00:45.970 --> 00:46.650
A You are right.

00:46.660 --> 00:49.360
To which the client should be redirected.

00:49.360 --> 00:56.700
For example at the moment we can access the demo sites image by requesting thumb dot P in G.

00:57.100 --> 01:04.600
But let's say we also want to serve this image for Slash logo which obviously right now returns a foro

01:04.600 --> 01:09.620
for we can create a simple redirect using the return directive.

01:09.930 --> 01:12.900
I'll get rid of all these bits from the previous lesson.

01:12.970 --> 01:15.600
Change this location to that slash logo.

01:15.610 --> 01:16.660
You are right.

01:16.960 --> 01:25.500
And in this location return a 3 0 7 so a temporary redirect again or 300 status codes being redirects

01:25.500 --> 01:26.830
of some sort.

01:26.950 --> 01:30.060
And what we want to return is slash thumb.

01:30.070 --> 01:31.720
Dot P in G.

01:31.720 --> 01:33.370
We could also have made this a complete.

01:33.370 --> 01:36.150
You are right with protocol host etc..

01:36.370 --> 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

01:41.830 --> 01:50.380
from save this reload the configuration and now when we reload this request for Slash logo we get the

01:50.380 --> 01:52.280
thumb doppie G image.

01:52.570 --> 01:58.050
But very importantly note that the u r l changed to thumb don't be in G.

01:58.210 --> 02:04.270
Which is essentially the main difference between rewrites and redirects a redirect simply tells the

02:04.270 --> 02:05.730
client performing the request.

02:05.740 --> 02:08.610
Where to go instead a rewrite.

02:08.620 --> 02:12.240
On the other hand mutates the you are right internally.

02:12.310 --> 02:18.730
So let's see an example this time in the server context before matching the request to any location

02:18.730 --> 02:19.910
contexts.

02:20.050 --> 02:22.070
We can say rewrite.

02:22.240 --> 02:26.280
Pass a regular expression with which to match the you are I.

02:26.500 --> 02:34.540
I'll say Carrott So starting with Slash user slash and then W. plus.

02:34.750 --> 02:42.100
So more than one word character with the second argument being the new you are try to rewrite this to

02:42.550 --> 02:44.540
slash greet.

02:44.550 --> 02:47.940
Now here's the important thing to understand about rewrites.

02:48.190 --> 02:55.630
When you are IE is rewritten it also gets re-evaluated by engine X as a completely new request meaning

02:55.690 --> 02:57.280
at this point here.

02:57.520 --> 03:04.020
The rewritten greet you are I start to right from the top again and will get re-evaluated again.

03:04.210 --> 03:11.380
The re-evaluation makes rewrites very powerful but also requires more system resources than say a return.

03:11.380 --> 03:14.680
Just something to keep in mind when creating rewrite rules.

03:14.890 --> 03:21.700
Okay once rewritten to slash greet this request will move past this rewrite so let's create a location

03:21.700 --> 03:23.170
to match add on.

03:23.170 --> 03:34.030
I'll change this one and return a simple 200 worth Hello user save reload the configuration.

03:34.030 --> 03:36.460
Keeping in mind what our rewrite checks for.

03:36.460 --> 03:43.210
I'll request slash user slash the first part of that rewrite rule John.

03:43.240 --> 03:45.700
The second part with the word match.

03:45.940 --> 03:46.670
Enter.

03:46.960 --> 03:48.520
Hello user.

03:48.520 --> 03:55.570
So clearly the response from slash greet although the browser is still pointing to slash user slash

03:55.570 --> 04:02.290
John meaning the request was rewritten internally only unbeknownst to the client.

04:02.290 --> 04:08.130
Another important and powerful feature of rewrites is the ability to capture certain parts of the original.

04:08.130 --> 04:09.010
You are right.

04:09.340 --> 04:15.840
Using standard regular expression capture groups or example let's say we want to capture a user name.

04:15.940 --> 04:22.870
This word character section of the regular expression simply wrap that part in braces like so.

04:23.110 --> 04:29.320
Which being the first capshaw group in this you are they can be accessed as dollar signs so a variable

04:29.500 --> 04:32.960
one should we have had a second caption Ruby here.

04:32.980 --> 04:38.990
Like this we could have access that value as dollar sign too and so on.

04:38.990 --> 04:46.000
OK so with this variant we're now requesting slash user slash John as we did in the browser and then

04:46.000 --> 04:52.570
rewriting that to slash greet slash John as that will be the value of this capture group.

04:52.570 --> 04:58.990
We can test this by creating an exact match location specifically for Slash greet slash John meaning

04:59.050 --> 05:03.600
any other user will be matched on the standard prefix match and only John.

05:03.710 --> 05:08.860
This exact match returned 200 Hello John.

05:09.800 --> 05:11.730
Reload these changes.

05:11.730 --> 05:18.980
And now what should happen when I refresh this is that slash user slash John should get rewritten to

05:18.980 --> 05:27.320
slash greet slash John revaluated as that new you are II so skipping past the rewrite and then even

05:27.320 --> 05:33.620
though this slash Greek location is a match I was slash greets slash John exact match will take priority

05:33.650 --> 05:34.320
and return.

05:34.340 --> 05:35.370
Hello John.

05:36.080 --> 05:37.020
Test it.

05:37.780 --> 05:39.300
And it works.

05:39.310 --> 05:40.800
Change this to any other user.

05:40.810 --> 05:41.780
However.

05:41.800 --> 05:42.640
Jane.

05:43.400 --> 05:46.110
And we get the prefix match with Hello user.

05:47.390 --> 05:48.800
Get the last part of rewrites.

05:48.810 --> 05:55.860
I'm going to cover is passing optional flags or one in particular the last flag to demonstrate.

05:55.880 --> 05:57.750
OK read another rewrite here.

05:57.840 --> 06:04.550
This time matching slash greet slash John and rewriting it to thumb dot B in G.

06:04.590 --> 06:06.220
The demo site image.

06:06.540 --> 06:13.260
Meaning that if we now request slash user slash John this first rewrite will matchett rewrite it to

06:13.260 --> 06:20.010
slash graetz lashed John and then have it re-evaluate it where it will then match the second rewrite

06:20.070 --> 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.

06:30.410 --> 06:32.170
Request John again.

06:33.930 --> 06:39.140
And sure enough we get the thumb image under this user slash John request.

06:39.160 --> 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

06:44.670 --> 06:49.510
rewritten any more on this first rewrite add lost.

06:49.560 --> 06:54.960
So still re-evaluate the new you are right after the rewrite but make sure this is the last time it

06:54.960 --> 07:03.140
gets rewritten meaning it should skip over the second rewrite save reload request that same you are

07:03.140 --> 07:06.560
row with a reload and we're back to hello John.

07:07.680 --> 07:10.170
That wraps up redirects and rewrites.

07:10.200 --> 07:16.700
Although technically there's a third kind of rewrite called Try files which will cover in the next lesson.
