RyanJ RyanJ - 10 months ago 85
Apache Configuration Question

Rewrite all requests to PHP script on Nginx

Im new with Nginx and for my project I need to rewrite all requests to the index.php, which is the request handler.
I done this with .htaccess file in Apache but now I wanted to make it compatible also with Nginx. I know that on Nginx don't exist .htaccess files so I have to edit the virtual host file in /etc/nginx/sites-available/default but I dont know which is the Nginx equivalent of my old Apache rules.
Here's the content of my old .htaccess file:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/index\.php
RewriteRule ^([^?]*)$ /index.php [QSA,NS,L]

With this on Apache all requests was rewritten to index.php, no matter if these are PHP scripts, static files or inexistent files (in my project there are also some virtual paths like /::API/ or /::ADMIN/ and the root shorcut /~/), how can I do that with Nginx?


The nginx docs lay out the rewrite directive: http://nginx.org/en/docs/http/ngx_http_rewrite_module.html

The simple answer is something like this:

location / {
    rewrite ^(.*)$ /index.php?$1 last;

Edited to add the ?$1 bit. Thanks, @bob0t