在NGINX conf中外部重定向URL以添加斜杠并使用php文件

在NGINX conf中外部重定向URL以添加斜杠并使用php文件,php,apache,.htaccess,nginx-reverse-proxy,Php,Apache,.htaccess,Nginx Reverse Proxy,我是这个网站的新成员,也是NGINX的新成员,所以请友好一点。我最近切换到AWS Elasticbeanstalk,需要NGINX配置方面的帮助。我想做一些我知道并测试过的重定向,可以在apache2中与.htaccess一起工作,但我无法完成NGINX 我希望我的所有URL都以trainling斜杠结尾,检查$uri是否是目录,如果是,请使用index.php,如果不是,请使用last name作为脚本名称,如下所示: 我希望此URL:使用以下脚本: https://localhost/ ->

我是这个网站的新成员,也是NGINX的新成员,所以请友好一点。我最近切换到AWS Elasticbeanstalk,需要NGINX配置方面的帮助。我想做一些我知道并测试过的重定向,可以在apache2中与.htaccess一起工作,但我无法完成NGINX

我希望我的所有URL都以trainling斜杠结尾,检查$uri是否是目录,如果是,请使用index.php,如果不是,请使用last name作为脚本名称,如下所示:

我希望此URL:使用以下脚本:

https://localhost/ ->/index.php

https://localhost/page/ ->//page.php

https://localhost/folder/page/ ->./folder/page.php

https://localhost/folder/page/?param=foo ->./folder/page.php?param=foo

我在apache2中使用.htaccess中的以下配置使其行为与此类似:

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]

# add a trailing slash   
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
如何将这个.htaccess转换为NGINX配置文件


提前感谢。

尝试使用此功能,请记住在插入后重新启动:

location / {
    try_files $uri $uri.html $uri/ @extensionless-php;
    index index.html index.htm index.php;
}

location ~ \.php$ {
    try_files $uri =404;
}

location @extensionless-php {
    rewrite ^(.*)$ $1.php last;
}

不幸的是,这不能正常工作。不以.php结尾的请求以strage方式重定向。例如:->@salvamiguel我想我误解了你的帖子,我已经相应地更新了答案。