1

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

I tried

rewrite ^/(.*)/$ /$1 permanent;

But this work only for http://test.com/anything/ not for root

Thanks a lot

recursive
  • 11
  • 1

1 Answers1

1

There is no such thing as a domain requested "without slash".

When you go to https://example.com, your browser will always fetch some URI (path) from the server, and by default, it is /.

So you are simply asking for an impossible thing :-)

P.S. whether a browser displays the trailing slash when a "domain" is requested, varies. For example, Chrome would hide trailing slash for known TLDs while displaying it for local TLDs, e.g.

https://example.com/ => trailing slash hidden

http://example.local/ => trailing slash is shown

Danila Vershinin
  • 8,725
  • 2
  • 29
  • 35
  • That's not exactly correct. If you open example.com without trailing slash (or anything else after domain), you won't see your request in nginx logs, while if you open example.com/, it will be logged as expected. Both Chrome and FireFox do not add / to URL – Vitaly Feb 15 '22 at 15:28
  • @Vitaly "you won't see your request in nginx logs" it's incorrect. Whether the request is "visually" with or without slash in your browser's address bar, has no reflection whatsoever on how the request is made to the server: with a slash, and the logging is up to your web server configuration. Your statement is incorrect. – Danila Vershinin Feb 16 '22 at 06:36