Zhang Buzz
Zhang Buzz

Reputation: 11088

How to redirect only the root path in 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/.

Upvotes: 41

Views: 34010

Answers (2)

Ikrom
Ikrom

Reputation: 5123

I found another similar solution, I think it is more concise:

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

Link to source

Upvotes: 64

Zhang Buzz
Zhang Buzz

Reputation: 11088

I got it.

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

Upvotes: 61

Related Questions