Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to redirect only the root path in nginx?

Tags:

redirect

nginx

I only want to redirect the root path from domain A to domain B. For example, if user type in https://www.a.com/ or https://www.a.com or http://a.com all redirect to https://www.b.com/, but if user type in https://www.a.com/something/ then it keep there without redirect.

I tried the following:

location / {     return 301 https://www.b.com/; } 

but it redirect all to www.b.com even user type in https://www.a.com/something/.

like image 410
Zhang Buzz Avatar asked Jan 20 '17 02:01

Zhang Buzz


People also ask

What is Nginx permanent redirect?

Permanent redirects such as NGINX 301 Redirect simply makes the browser forget the old address entirely and prevents it from attempting to access that address anymore. These redirects are very useful if your content has been permanently moved to a new location, like when you change domain names or servers.

How do I change my URL in nginx?

NGINX Return directive The easiest and cleaner way to rewrite an URL can be done by using the return directive. The return directive must be declared in the server or location context by specifying the URL to be redirected.


1 Answers

I got it.

location ~ ^/$ {     return 301 https://www.b.com/; } 
like image 102
Zhang Buzz Avatar answered Oct 05 '22 16:10

Zhang Buzz