【问题标题】:nginx redirect .php URIs to friendly counterpartnginx 将 .php URI 重定向到友好的对应对象
【发布时间】:2015-10-06 06:16:43
【问题描述】:

我目前正在使用重写来接受不包含 .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;
}

【问题讨论】:

  • 嗨,虽然我知道您想重定向到您的 php 文件,但为了实现您想要的功能,不涉及或相关 php,请删除 php 标签,因为它不相关:) (或更新您的问题以包含与 php 相关的内容)

标签: linux redirect nginx


【解决方案1】:

如果你已经工作了,我想你要找的是这部分:

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 中提供了更多解释。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-01
    • 2016-01-03
    • 2014-02-24
    • 2020-07-17
    • 2016-01-06
    相关资源
    最近更新 更多