Nginx-重定向域尾随点 [英] Nginx - Redirect Domain Trailing Dot

查看:108
本文介绍了Nginx-重定向域尾随点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何重定向" http://domain.com ."到Nginx的" http://domain.com "?

How can I redirect "http://domain.com." to "http://domain.com" with Nginx?

推荐这样做的方法是什么?正则表达式还是还有其他选择?

What's the recommended way of doing this? Regex or is there any other options?

推荐答案

以下代码段以一般方式执行此操作,而无需对任何主机名进行硬编码(如果服务器配置处理多个域的请求,则很有用).将此添加到所需的任何服务器定义中.

The following snippet does this in a general way, without having to hard code any hostnames (useful if your server config handles requests for multiple domains). Add this inside any server definition that you need to.

if ($http_host ~ "\.$" ){
    rewrite ^(.*) $scheme://$host$1 permanent;
}

这利用了以下事实( Igor Sysoev 指出)的事实>删除了结尾的点,而$http_host没有删除;因此我们可以匹配$http_host中的点,并自动使用$host进行重定向.

This takes advantage of the fact (pointed out by Igor Sysoev) that $host has the trailing dot removed, while $http_host doesn't; so we can match the dot in $http_host and automatically use $host for the redirect.

这篇关于Nginx-重定向域尾随点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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