1

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
RewriteCond %{ENV:REDIRECT_STATUS} ^$
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?

RyanJ
  • 131
  • 5
  • 16

1 Answers1

3

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

Chazzu
  • 840
  • 7
  • 5
  • you forgot the QSA Flag : this gives something more like `/index.php$query_string` – Bobot Aug 11 '16 at 13:10
  • I have this in my file: `server { location / { rewrite ^(.*)$ /index.php$query_string last; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php7.0-fpm.sock; fastcgi_index index.php; include fastcgi_params; } }` But I got 502: Bad Gateway on PHP scripts and 404 on virtual paths (inexistent files), I wrong something? – RyanJ Aug 11 '16 at 13:18
  • Check my edit. Bob0t was saying what the result would be, not the change to make. – Chazzu Aug 11 '16 at 13:20
  • I've done the edit, so now the block is: `location / { rewrite ^(.*)$ /index.php?$1 last; } location ~ \.php$ { try_files $uri =404; fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_pass unix:/var/run/php7.0-fpm.sock; fastcgi_index index.php; include fastcgi_params; }` But still the same errors – RyanJ Aug 11 '16 at 16:04
  • I think the try_files bit is causing it to 404. Take a look at this: http://stackoverflow.com/questions/12924896/rewrite-all-requests-to-index-php-with-nginx There's a pretty comprehensive answer there. – Chazzu Aug 11 '16 at 16:28
  • 1
    Thanks, I've checked the question and I've understood the error, here the corrected configuration block, maybe could be useful also for someone else: `location / { rewrite ^(.*)$ /index.php?$1 last; } location ~ \.php$ { fastcgi_pass unix:/var/run/php/php7.0-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME /[PATH TO WEBSITE]/index.php; }` – RyanJ Aug 11 '16 at 17:09
  • You might also wanna set the querystring name, like so: `rewrite ^(.*)$ /index.php?uri=$1 last;`, so in PHP you can get it with `$_GET['uri']` directly. – Julien Jun 10 '22 at 02:53