nginx将.php URI重定向到友好的副本 [英] nginx redirect .php URIs to friendly counterpart

查看:76
本文介绍了nginx将.php URI重定向到友好的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用重写来接受文件请求,但不包括.php扩展名,但是,我不知道如何拒绝或重定向具有.php的请求.

I'm currently using a rewrite to accept requests for files without including the .php extension, however, I can't find out how to have the requests with .php either be denied or redirected to the friendly version.

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

For example, this is what I want to accomplish: /contact.php REDIRECT /contact /contact.php (/contact.php exists, but only accessible via /contact) would result in a 403 error

当前配置:

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;
}

推荐答案

提供了您已经使用的功能,我想您正在寻找的是以下部分:

Provided that what you have already works, I think what you're looking for is this part:

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

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

Or, if you're using query parameters, too:

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

您应将其放置在\.php$位置(应从当前的.php重命名).相信我,它不会产生无限循环!

You should place it within your \.php$ location (which you should rename from your present .php). Trust me, it's NOT going to generate an infinite loop!

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

这篇关于nginx将.php URI重定向到友好的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆