I have a website I need to move from an Apache server to an Nginx server.
The .htaccess file of this website is as follows
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ $1.php [L]
This redirects www.example.com/hello to www.example.com/hello.php keeping the original link, but omiting the .php from the end of the url.
I tried to replicate this in my Nginx server block..
location / {
# Check if a file exists, or route it to index.php.
try_files $uri $uri/ /index.php;
}
rewrite ^/([a-z]+)$ /$1.php last;
The rewrite rule works, redirecting all links to the .php file, but breaks the Nginx try_files rule for searching inside a directory if the file does not exist.
The below admin example link is a folder with an index.php file inside it.
www.example.com/admin
The try_files should redirect this to..
www.example.com/admin/
Since there is no file called admin.php. Instead I get the 'No input file specified' message.
When I remove the rewrite, the try_files works correctly. However the rest of the site does not work without this rewrite rule.
Does anyone know how I can get the try_files and the rewrite to work together?
Thanks
Ben