Now that we have an understanding of engine X variables we can move on to rewrites or simply redirect.
In some cases the two directives we can use for rewriting requests is the rewrite directive and the
return directive which we've already used several times throughout this course and being the same bluff.
The two very importantly however the return statement as we've been using it takes a status code and
response data or strings.
But in the case of that response code being a 300 variant which is four redirects the return directives
behaviour changes in it then except a you are dry as the second parameter.
A You are right.
To which the client should be redirected.
For example at the moment we can access the demo sites image by requesting thumb dot P in G.
But let's say we also want to serve this image for Slash logo which obviously right now returns a foro
for we can create a simple redirect using the return directive.
I'll get rid of all these bits from the previous lesson.
Change this location to that slash logo.
You are right.
And in this location return a 3 0 7 so a temporary redirect again or 300 status codes being redirects
of some sort.
And what we want to return is slash thumb.
Dot P in G.
We could also have made this a complete.
You are right with protocol host etc..
But a relative you are I will be fine as this is redirecting to the same host as the request this coming
from save this reload the configuration and now when we reload this request for Slash logo we get the
thumb doppie G image.
But very importantly note that the u r l changed to thumb don't be in G.
Which is essentially the main difference between rewrites and redirects a redirect simply tells the
client performing the request.
Where to go instead a rewrite.
On the other hand mutates the you are right internally.
So let's see an example this time in the server context before matching the request to any location
contexts.
We can say rewrite.
Pass a regular expression with which to match the you are I.
I'll say Carrott So starting with Slash user slash and then W. plus.
So more than one word character with the second argument being the new you are try to rewrite this to
slash greet.
Now here's the important thing to understand about rewrites.
When you are IE is rewritten it also gets re-evaluated by engine X as a completely new request meaning
at this point here.
The rewritten greet you are I start to right from the top again and will get re-evaluated again.
The re-evaluation makes rewrites very powerful but also requires more system resources than say a return.
Just something to keep in mind when creating rewrite rules.
Okay once rewritten to slash greet this request will move past this rewrite so let's create a location
to match add on.
I'll change this one and return a simple 200 worth Hello user save reload the configuration.
Keeping in mind what our rewrite checks for.
I'll request slash user slash the first part of that rewrite rule John.
The second part with the word match.
Enter.
Hello user.
So clearly the response from slash greet although the browser is still pointing to slash user slash
John meaning the request was rewritten internally only unbeknownst to the client.
Another important and powerful feature of rewrites is the ability to capture certain parts of the original.
You are right.
Using standard regular expression capture groups or example let's say we want to capture a user name.
This word character section of the regular expression simply wrap that part in braces like so.
Which being the first capshaw group in this you are they can be accessed as dollar signs so a variable
one should we have had a second caption Ruby here.
Like this we could have access that value as dollar sign too and so on.
OK so with this variant we're now requesting slash user slash John as we did in the browser and then
rewriting that to slash greet slash John as that will be the value of this capture group.
We can test this by creating an exact match location specifically for Slash greet slash John meaning
any other user will be matched on the standard prefix match and only John.
This exact match returned 200 Hello John.
Reload these changes.
And now what should happen when I refresh this is that slash user slash John should get rewritten to
slash greet slash John revaluated as that new you are II so skipping past the rewrite and then even
though this slash Greek location is a match I was slash greets slash John exact match will take priority
and return.
Hello John.
Test it.
And it works.
Change this to any other user.
However.
Jane.
And we get the prefix match with Hello user.
Get the last part of rewrites.
I'm going to cover is passing optional flags or one in particular the last flag to demonstrate.
OK read another rewrite here.
This time matching slash greet slash John and rewriting it to thumb dot B in G.
The demo site image.
Meaning that if we now request slash user slash John this first rewrite will matchett rewrite it to
slash graetz lashed John and then have it re-evaluate it where it will then match the second rewrite
and again be rewritten to thumb doppie in G and finally served as thumb dont be in G save and test this.
Request John again.
And sure enough we get the thumb image under this user slash John request.
Now what the last flag allows us to do is essentially tell engine X not to allow you are right to be
rewritten any more on this first rewrite add lost.
So still re-evaluate the new you are right after the rewrite but make sure this is the last time it
gets rewritten meaning it should skip over the second rewrite save reload request that same you are
row with a reload and we're back to hello John.
That wraps up redirects and rewrites.
Although technically there's a third kind of rewrite called Try files which will cover in the next lesson.