gpt4 book ai didi

linux - nginx 将 .php URI 重定向到友好的对应方

转载 作者:太空宇宙 更新时间:2023-11-04 13:04:52 25 4
gpt4 key购买 nike

我目前正在使用重写来接受不包含 .php 扩展名的文件请求,但是,我无法找到如何拒绝带有 .php 的请求或将其重定向到友好版本。

例如,这是我想要完成的:/contact.php 重定向/contact/contact.php(/contact.php 存在,但只能通过/contact 访问)会导致 403 错误

当前配置:

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

location ~ .php {
try_files $uri =404;
fastcgi_pass backend;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}

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

最佳答案

前提是你已经能用了,我想你要找的就是这部分:

if ($request_uri ~ ^/(.*)\.php$) {  return 301 /$1;  }

或者,如果您也使用查询参数:

if ($request_uri ~ ^/([^?]*)\.php($|\?)) {  return 301 /$1?$args;  }

你应该把它放在你的 \.php$ 位置(你应该从你现在的 .php 重命名)。相信我,它不会生成无限循环!

https://serverfault.com/questions/568569/nginx-url-rewrite-rule/568902#568902 中提供了更多解释.

关于linux - nginx 将 .php URI 重定向到友好的对应方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32962965/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com