The strategy of the last person telling the person in front the colour of that person's hat, would work if they were the only two people. Working from the simplest case in which this strategy does not work:
x x x x
b b r r
b r b r
(Since only "blue" or "red" may be uttered, then the one person to die must be the last person, since they know about the hats in front of them, and can pass this information forward. Their hat colour is irrelevant.)
The "x" row represents the person at the back of a line. These are all the possible configurations that the other two people could be in.
Now, it is not known in which configuration the line is in. The person at the back must be able to signal to the others which configuration the line is in, using only "blue" or "red". Let's assume that "blue", for now, means that a specific condition is "true", while "red" means "false".
If we look at these four possibilities in terms of their differences, we can see that the leftmost and rightmost line has two of the same colour, while the middle two has different colours. Let's say that the condition is specified as "the two hats in front of me, the last person in line, are both the same colour".
t f f t
b b r r
b r b r
Colour-coded for your convenience.
Now the second person in line knows the configuration of the line. If this person heard "blue" (true) as a response, then they would know that their hat is the same colour as the hat in front of them, and the first person in line would know this as well. Otherwise, if the second person heard "red", then they would name the opposite colour as the hat in front of them, and the first person would also give the correct answer.
This answer, when expanded to 4 people in a line, doesn't work. So we'll redefine the conditions a bit, to fit with the results that already work. The condition is, "there are an even number of blue hats in front of me (0 is considered even in this case)".
Looking at the same case, we now come to the second-in-line's answer. This person would know from last-in-line's answer whether the line had an even number of blue hats, or not. If second-in-line knows whether there are an even number of blue hats or not, then they would also be able to deduce the colour of their own hat after looking at the colour in front.
Their answers would therefore be:
e o o e
o e o e
b r b r
Second-in-line gives an answer of "blue" when they recognize that they have the opposite result of even or odd as last-in-line. First-in-line, following the same rule, would also have the correct answer. Notice that after an answer of "blue", the meaning of the word for second-in-line changes from "true" to "false". If we look at a line of say, eight people, we get:
Answer/
Colour of hat Even number of blue hats in front? Agree with previous? "True" colour
b (true) x b
b (false) no r
b (true) no b
r (true) yes r
b (false) no r
r (false) yes b
r (false) yes b
b (true) no b
So the rules:
1. Last-in-line says "blue" if there is an even number of blue hats in front of them, otherwise, says red. blue = true, red = false.
2. After each answer of "blue", the meanings of the colours switch. If last-in-line says "blue", then second-in-line must remember that "blue" is now "false" and "red" is now "true". The meanings are kept track of by each person down the line.
3. Every person in front of last-in-line sees whether there is an even number of blue hats in front of them (true or false), and compares their answer to the previous answer (the previous answer is true if the colour named matches the previous "true" colour), if the two do not agree, then they must answer "blue". Otherwise, they answer "red". Their answer, along with the current "true" colour, is passed on, so that the next person can use the same reasoning to correctly state their own hat colour.
If I missed something, and this solution is wrong, please tell me. I've been looking at it for a while, so I can't tell if it actually makes sense or not.