How it works...

Like our non-www redirect, this recipe works in the same fashion. In fact, a redirect for a subdomain isn't functionally any different from a completely different domain altogether.

The return rule works just the same, where calls to subdirectories and all arguments are passed to the new domain without breaking their functionality. For example, using our preceding code, if we made a call to http://oldsite.nginxcookbook.com/pages/?item=1951, it would send a 301 redirect to http://newsite.nginxcookbook.com/pages/?item=1951.