nginx - how do I get rewrite directives to execute before index directives?
- by Daniel Hai
I'm trying a simple internal rewrite with nginx to navigate to a sub-directory depending on the user_agent -- mobile browsers go to /mobile, otherwise they go to /www
however it seems that when I rewrite these urls, the index directive is processed before the rewrites, so I end up getting 403 forbidden.
# TEST FOR INDEX
index index.php
# TEST PHONES
if ($http_user_agent ~* '(iPhone|iPod)') {
rewrite ^(.*)$ /mobile$1 break;
}
# OTHERWISE WE ARE DONE
rewrite ^(.*)$ /www$1 break;
when I turn off the re-writes and hit the hostname (http://www.somehost.com/) the index is displayed correctly. When they are on, I have to explicitly navigate to somehost.com/index.php to get the script to run ...
Do I have to explicity test for directories, and then re-write to an index.php file, or is there a simpler solution?