rewrite runs before access phase, and since you rewrite to another location, you never reach the access phase for the first location. See: https://cloud.githubusercontent.com/assets/2137369/15272097/77d1c09e-1a37-11e6-97ef-d9767035fc3e.png
2 solutions:
1) use rewrite_by_lua and put your validation logic and rewrite logic there. no access_by_lua needed.
2) use access_by_lua and put your validation logic and rewrite logic there. no rewrite_by_lua needed.