1

Can anybody help understand why there is ^ and $? I known ^ means "start with", $ means "end with". I just don't know why we need that? Could you please list some URIs that match the following rewrite and show me the difference with or without the two symbols?

rewrite ^/users/(.*)$ /show?user=$1? last;
Vidy Videni
  • 1,686
  • 2
  • 11
  • 19

1 Answers1

4

These are present to ensure that the entire URL is matched when performing the rewrite.

For example, without the ^, these URLs would match:

/admin/users/foo
/whatever/users/bar

In this case, the $ isn't really required because the * is greedy (. matches anything and the * matches as much as possible). This results in the expression matching the remaining input even without the $. In a more restricted case such as:

rewrite ^/users/dan$ /show?user=dan last;

The $ is important for the same reason. Without it, these URLs would match:

/users/dan/delete
/users/dan/profile
/users/danny

But with it, only the exact url /users/dan would match.

Dark Falcon
  • 41,222
  • 5
  • 76
  • 92
  • the reason why the ^/users/(.*)$ makes me confused is I think every request URI starts with /, now I understand ^/users means " a request URI starts with the whole part "/users" " , another reason is I don't know why it still adds $, I also think it is not necessary in this context, You have solved my years confusion. thanks. – Vidy Videni Sep 25 '14 at 02:14