# Redirect ALL requests to single PHP file

**URL:** <https://www.ruby-forum.com/t/redirect-all-requests-to-single-php-file/215885>\
**Category:** NGINX\
**Created:** [January 18, 2012, 5:35pm UTC](https://www.ruby-forum.com/t/redirect-all-requests-to-single-php-file/215885 "2012-01-18T17:35:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luislavena](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/0ea827/32.png) [@luislavena](https://www.ruby-forum.com/u/luislavena)\
**Post date:** [January 18, 2012, 5:35pm UTC](https://www.ruby-forum.com/t/redirect-all-requests-to-single-php-file/215885/1 "2012-01-18T17:35:47Z")

</div>

Hi All,

We’d like to redirect all requests to a single PHP file. The  
configuration below is what we have but I’m concerned that it would  
cause an “infinite” loop and possibly other problems. We’d like to  
redirect ANY request to api.php regardless if the file exists or not.  
Even a request to an existing static file should rewrite to the  
“api.php” file.

Is this the correct way? is there a better way?

Thanks

```
    server
    {
            listen #.#.#.#:80;
            server_name api.domain.com;

            rewrite ^(.*)$ /api.php?uri=$1 last;

            location ~ \.php$
            {
                    include fcgi;
                    fastcgi_pass 127.0.0.1:9000;
            }
    }

```

Posted at Nginx Forum:

> **[Redirect ALL requests to single PHP file](https://forum.nginx.org/read.php?2,221398,221398#msg-221398)**
>
> Hi All, We'd like to redirect all requests to a single PHP file. The configuration below is what we have but I'm concerned that it would cause an "infinite" loop and possibly other problems. We'd like to redirect ANY request to api.php...

---

<div class="post-metadata">

**Author:** ![iberkner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c67d28/32.png) [@iberkner](https://www.ruby-forum.com/u/iberkner)\
**Post date:** [January 18, 2012, 5:46pm UTC](https://www.ruby-forum.com/t/redirect-all-requests-to-single-php-file/215885/2 "2012-01-18T17:46:17Z")

</div>

On 18 Jan 2012 16h35 WET, [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Hi All,
> 
> We’d like to redirect all requests to a single PHP file. The  
> configuration below is what we have but I’m concerned that it would  
> cause an “infinite” loop and possibly other problems. We’d like to  
> redirect ANY request to api.php regardless if the file exists or  
> not. Even a request to an existing static file should rewrite to  
> the “api.php” file.
> 
> Is this the correct way? is there a better way?

Try:

location = /api.php {  
internal; # comment this out to allow direct access to api.php.  
include fcgi;  
fastcgi\_pass 127.0.0.1:9000;  
}

location / {  
return 302 /api.php?uri=$request\_uri;  
}

— appa

---

<div class="post-metadata">

**Author:** ![iberkner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/c67d28/32.png) [@iberkner](https://www.ruby-forum.com/u/iberkner)\
**Post date:** [January 18, 2012, 5:48pm UTC](https://www.ruby-forum.com/t/redirect-all-requests-to-single-php-file/215885/3 "2012-01-18T17:48:46Z")

</div>

Hello!

On Wed, Jan 18, 2012 at 11:35:24AM -0500, iberkner wrote:

> ```
> {
> include fcgi;
> fastcgi_pass 127.0.0.1:9000;
> }
> }
> 
> ```

server {  
listen …  
server\_name [api.domain.com](http://api.domain.com);

```
location / {
    fastcgi_pass 127.0.0.1:9000;
    fastcgi_param REQUEST_FILENAME $document_root/api.php;
    include fastcgi_params;
}

```

}

Maxim D.
