nginx - how do I get rewrite directives to execute before index directives?
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 the开发者_如何学Pythonn re-write to an index.php file, or is there a simpler solution?
try:
server {
index index.php;
location / {
if ($http_user_agent ~* '(iPhone|iPod)') {
rewrite ^(.*)$ /mobile$1 last;
}
rewrite ^(.*)$ /www$1 last;
}
}
This was an issue with a double call. Ooops. should've known.
The first request came in as / and then re-written to /www/.
The index was then applied, so the it then became /www/index.php, but the php handler was re-calling the rewrite rules, so the final url became: /www/www/index.php
精彩评论