Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx - Redirect Domain Trailing Dot

Tags:

nginx

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?

like image 388
user2168809 Avatar asked Mar 16 '13 17:03

user2168809


1 Answers

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

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.

like image 87
El Yobo Avatar answered Nov 15 '22 09:11

El Yobo