![](/img/trans.png)
[英]nginx redirect to trailing slashes and substitude to index.php (if files and folders don't exists)
[英]Externally redirect URL in NGINX conf to add slashes and use php files
我是这个网站的新手,也是 NGINX 的新手,所以请保持友善。 我最近切换到 AWS Elasticbeanstalk 并需要 NGINX 配置方面的帮助。 我想做一些我知道并在 apache2 中使用 .htaccess 测试过的重定向,但我无法使用 NGINX 完成。
我希望我的所有 URL 都以训练斜杠结尾,检查 $uri 是否是一个目录,如果是,则使用 index.php 如果不使用姓氏作为脚本的名称,如下所示:
我希望这个 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;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.