How to redirect to a different domain using NGINX?

RedirectNginxVhosts

Redirect Problem Overview


How can I redirect mydomain.com and any subdomain *.mydomain.com to www.adifferentdomain.com using NGINX?

Redirect Solutions


Solution 1 - Redirect

server_name supports suffix matches using .mydomain.com syntax:

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

or on any version 0.9.1 or higher:

server {
  server_name .mydomain.com;
  return 301 http://www.adifferentdomain.com$request_uri;
}

Solution 2 - Redirect

server {
    server_name .mydomain.com;
    return 301 http://www.adifferentdomain.com$request_uri;
}

http://wiki.nginx.org/HttpRewriteModule#return

and

http://wiki.nginx.org/Pitfalls#Taxing_Rewrites

Solution 3 - Redirect

Why use the rewrite module if you can do return? Technically speaking, return is part of the rewrite module as you can read here but this snippet is easier to read imho.

server {
    server_name  .domain.com;

    return 302 $scheme://forwarded-domain.com;
}

You can also give it a 301 redirect.

Solution 4 - Redirect

That should work via HTTPRewriteModule.

Example rewrite from www.example.com to example.com:

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

Solution 5 - Redirect

If you would like to redirect requests for "domain1.com" to "domain2.com", you could create a server block that looks like this:

server {
	listen 80;
	server_name domain1.com;
	return 301 $scheme://domain2.com$request_uri;
}

Solution 6 - Redirect

I'm using this code for my sites

server {
        listen 80;
        listen 443;
        server_name  .domain.com;

        return 301 $scheme://newdomain.com$request_uri;
}

Solution 7 - Redirect

You can simply write a if condition inside server {} block:

server { 

    if ($host = mydomain.com) {
        return 301 http://www.adifferentdomain.com;
    } 
}

Solution 8 - Redirect

Temporary redirect

rewrite ^ http://www.RedirectToThisDomain.com$request_uri? redirect;

Permanent redirect

rewrite ^ http://www.RedirectToThisDomain.com$request_uri? permanent;

In nginx configuration file for specific site:

server {    
    server_name www.example.com;
    rewrite ^ http://www.RedictToThisDomain.com$request_uri? redirect;

}

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
QuestiondebView Question on Stackoverflow
Solution 1 - RedirectkolbyjackView Answer on Stackoverflow
Solution 2 - RedirectSerhii TopolnytskyiView Answer on Stackoverflow
Solution 3 - RedirectRobin van BaalenView Answer on Stackoverflow
Solution 4 - RedirectudoView Answer on Stackoverflow
Solution 5 - RedirectZanMaxView Answer on Stackoverflow
Solution 6 - RedirectTechView Answer on Stackoverflow
Solution 7 - RedirectcryptoKTMView Answer on Stackoverflow
Solution 8 - RedirectTaimoor ChangaizView Answer on Stackoverflow