如果uri的第一部分不是文件,则nginx mod_redirect

我正在从Apache迁移到Nginx服务器,并试图找出如何用nginxconfigurationreplace我的.htaccess

在我重写我想启用漂亮的链接,并需要服务器来检查,如果第一部分不是一个文件,然后redirect到index.php否则redirect到该文件。

例:

我有两个PHP文件在根目录: index.phpfiles.php

 https://example.com/token/login ---> index.php/token/login https://example.com/index.php/token/login ---> index.php/token/login //same as above https://example.com/files.php/token/login ---> files.php/token/login 

我目前的/默认文件是

 server { listen 80 default_server; listen [::]:80 default_server; server_name vpn.simpleinformatics.com www.vpn.simpleinformatics.com; return 301 https://$server_name$request_uri; } server { # SSL configuration listen 443 ssl http2 default_server; listen [::]:443 ssl http2 default_server; include snippets/ssl-vpn.simpleinformatics.com.conf; include snippets/ssl-params.conf; root /var/www/html; index index.php index.html index.htm index.nginx-debian.html; server_name vpn.simpleinformatics.com; location / { # try_files $uri $uri/ =404; # try_files $uri $uri/ /index.php; try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.0-fpm.sock; } location ~ /\.ht { deny all; } location ~ /.well-known { allow all; } location /phpmyadmin { auth_basic "Admin Login"; auth_basic_user_file /etc/nginx/pma_pass; } } 

这将所有请求redirect到index.php ,甚至files.php/etc..请求:(。

那么如何告诉nginx检查URI的第一部分是否是文件,然后redirect到该文件。