i had a project in which i had to redirect all not found files to the index.php. I did this in apache with placing a .htaccess file in my project folder. the file's contents are--
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
now i wanted the same in nginx. here is how my nginx.conf looks like
root /usr/local/apache2/htdocs;
index index.php index.html index.htm;
location /project/ {
root /usr/local/apache2/htdocs;
index index.php index.html index.htm;
try_files $uri $uri/ index.php;
}
now whenever i make a request like http://localhost/project/hello
then this request should go to http://localhost/project/index.php
but this says File not found
.
Now why i thought this would work is because since the root
directive inside the location block overrides the root
directive outside the block (though there values are same), the rewrite module would look for /usr/local/apache2/htdocs/project/index.php
.
What am i missing here?
UPDATE
This configuration works
root /usr/local/apache2/htdocs;
location /project/ {
index index.php index.html index.htm; #effectively root here is /usr/local/apache2/htdocs
try_files $uri $uri/ /project/index.php;
}
But i dont see why this should work because as pointed out in the docs here directory matched in the location block is appended to the value of root
directive
Perhaps you needed to specify the root in the URL? Otherwise, there is no reference point for the URL. The server can't guess at whatever directory it was last in or the user intended. If that doesn't accomplish what you want, comment back and I'll spin up a server to do a quick test on.