41

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/.

2 Answers 2

65

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

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

Link to source

Sign up to request clarification or add additional context in comments.

2 Comments

= / form appears more intuitive and perhaps has become canonical.
This should be the accepted answer as it is cleaner and more lightweight than the regex.
61

I got it.

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

1 Comment

See other answer, no need for regex.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.