I had the following rule in the .htaccess
file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php?rt=$1 [L,B,QSA]
Now I've switched to Nginx and I converted it to this: (Using an online website)
location ~ "^/([\s\S]*)$" {
try_files $uri $uri/ /index.php?rt=$1;
}
It works in some cases, but it doesn't work exactly as expected. I mean $_GET['id']
would be Null
even when there is ?id=10
passed to the URL.
Now I'm trying to understand what RewriteCond %{REQUEST_FILENAME} !-f
means to be able to convert it to Nginx config syntax. Any idea how can I convert it to Nginx exactly the same?
The web server converts the URL into a pathname for the requested file. This is stored in the variable REQUEST_FILENAME
. The RewriteCond
statement in your question asserts that this file exists. If the pathname does not exist as a file or directory, then the request is passed to index.php
.
In Nginx, this is equivalent to try_files $uri $uri/ /index.php;
- the $uri
term tests if the file exists, the $uri/
term tests if the directory exists, and the final parameter is the action taken if both tests fail.
In Apache, the QSA
flag in the RewriteRule
ensures that the requested query string is also appended to the rewritten URL. Your try_files
snippet isn't doing that! You are replacing the query string with rt=$1
.
Try:
try_files $uri $uri/ /index.php?rt=$1&$args;