nginx.conf redirect multiple conditions

RedirectNginx

Redirect Problem Overview


I want to redirect requests on two conditions using nginx.

This doesn't work:

  if ($host = 'domain.com' || $host = 'domain2.com'){
    rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
  }   

What is the correct way to do this?

Redirect Solutions


Solution 1 - Redirect

I had this same problem before. Because nginx can't do complex conditions or nested if statements, you need to evaluate over 2 different expressions.

set a variable to some binary value then enable if either condition is true in 2 different if statements:

set $my_var 0;
if ($host = 'domain.com') {
  set $my_var 1;
}
if ($host = 'domain2.com') {
  set $my_var 1;
}
if ($my_var = 1) {
  rewrite ^/(.*)$ http://www.domain.com/$1 permanent;
}   

Solution 2 - Redirect

The correct way would be to use a dedicated server for the redirect:

server {
  server_name domain.com domain2.com;
  rewrite ^ http://www.domain.com$request_uri? permanent;
}

Solution 3 - Redirect

another possibility would be

server_name domain.com domain2.com;
set $wanted_domain_name domain.com;
if ($http_host != $wanted_domain_name) {
    rewrite  ^(.*)$  https://$wanted_domain_name$1;
}

so it will redirect all to one specific but it's based on the usecase i guess

Solution 4 - Redirect

Here's a declarative approach:

server {
	listen       80;
	server_name  domain.com domain2.com;
	return 301 $scheme://www.domain.com$uri;    
}

server {
	listen       80  default_server;
	server_name  _;
	#....
}

Solution 5 - Redirect

>Rewriting multiple domains to a single domain and avoiding a looping condition in the browser. >-------------------------------------------------------------------------------------------

server {
    listen       80;
    server_name  www.wanted_domain.com wanted_domain.com www.un_wanted_domain.com un_wanted_domain.com;
    if ($host = 'un_wanted_domain.com'){
	   return 301 $scheme://www.wanted_domain.com$request_uri;
    }
    if ($host = 'www.un_wanted_domain.com'){
	   return 301 $scheme://www.wanted_domain.com$request_uri;
    }

Solution 6 - Redirect

I think the easiest way to do it it's just use regular expression:

if ($host ~ "domain.com|domain2.com") {
    rewrite ^/(.*)$ http://www.example.com/$1 permanent;
}

But it's good only when you have only strings; for complex logic, sure, it is not correct.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmarkView Question on Stackoverflow
Solution 1 - RedirectChris BarrettoView Answer on Stackoverflow
Solution 2 - RedirectkolbyjackView Answer on Stackoverflow
Solution 3 - RedirectshadowdroidView Answer on Stackoverflow
Solution 4 - RedirectCole TierneyView Answer on Stackoverflow
Solution 5 - RedirectDavidView Answer on Stackoverflow
Solution 6 - RedirectNikita_kharkov_uaView Answer on Stackoverflow