How do I make Nginx redirect all requests for files which do not exist to a single php file?
Posted
by
Richard
on Server Fault
See other posts from Server Fault
or by Richard
Published on 2011-01-05T14:33:19Z
Indexed on
2011/01/05
14:55 UTC
Read the original article
Hit count: 359
I have the following nginx vhost config:
server {
listen 80 default_server;
access_log /path/to/site/dir/logs/access.log;
error_log /path/to/site/dir/logs/error.log;
root /path/to/site/dir/webroot;
index index.php index.html;
try_files $uri /index.php;
location ~ \.php$ {
if (!-f $request_filename) {
return 404;
}
fastcgi_pass localhost:9000;
fastcgi_param SCRIPT_FILENAME /path/to/site/dir/webroot$fastcgi_script_name;
include /path/to/nginx/conf/fastcgi_params;
}
}
I want to redirect all requests that don't match files which exist to index.php. This works fine for most URIs at the moment, for example:
example.com/asd
example.com/asd/123/1.txt
Neither of asd
or asd/123/1.txt
exist so they get redirected to index.php and that works fine. However, if I put in the url example.com/asd.php
, it tries to look for asd.php
and when it can't find it, it returns 404 instead of sending the request to index.php
.
Is there a way to get asd.php
to be also sent to index.php
if asd.php
doesn't exist?
© Server Fault or respective owner