nginx: Rewrite PHP does not work

Posted by Ton Hoekstra on Server Fault See other posts from Server Fault or by Ton Hoekstra
Published on 2012-06-21T16:47:55Z Indexed on 2012/06/22 9:18 UTC
Read the original article Hit count: 250

Filed under:
|
|
|

I've a Suffix Proxy installed and I'm using the following rewrite with wildcard subdomain DNS on:

location / {
     if (!-e $request_filename) {
        rewrite ^(.*)$ /index.php last;
        break;
     }
}

My suffix proxy has the following URL format: (subdomain and/or domain + domain extension to proxy).proxy.org/(request-uri to proxy)

I've this php code in my index.php:

if(preg_match('#([\w\.-]+)\.example\.com(.+)#', $_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'], $match)) {
    header('Location: http://example.com/browse.php?u=http://'.$match[1].$match[2]);
    die;
}

But when requested a page with a .php extension I'll get a 404 not found error:

http://www.php.net.proxy.org/docs.php - HTTP/1.1 404 Not Found
http://www.utexas.edu.proxy.org/learn/php/ex3.php - HTTP/1.1 404 Not Found

But everything else is working (also index.php is working):

http://php.net.proxy.org/index.php - HTTP/1.1 200 OK
http://www.php-scripts.com.proxy.org/php_diary/example2.php3 - HTTP/1.1 200 OK
http://www.utexas.edu.proxy.org/learn/php/ex3.phps - HTTP/1.1 200 OK
http://www.w3schools.com.proxy.org/html/default.asp - HTTP/1.1 200 OK

Somebody has an answer? I don't know why it's not working, on apache it's working fine.

Thanks in advance.

I've removed the location and now it's working perfectly:

if (!-e $request_filename) {
     rewrite ^(.*)$ /index.php last;
     break;
}

© Server Fault or respective owner

Related posts about php

Related posts about nginx