How to redirect to a different domain using NGINX?
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;
}
server {
server_name .mydomain.example;
return 301 http://www.adifferentdomain.example$request_uri;
}
http://wiki.nginx.org/HttpRewriteModule#return
and
http://wiki.nginx.org/Pitfalls#Taxing_Rewrites
server_name supports suffix matches using .mydomain.example
syntax:
server {
server_name .mydomain.example;
rewrite ^ http://www.adifferentdomain.example$request_uri? permanent;
}
or on any version 0.9.1 or higher:
server {
server_name .mydomain.example;
return 301 http://www.adifferentdomain.example$request_uri;
}
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.