Redirect from any domain to specific domain using nginx

less than 1 minute read

Let’s say you want to make a 301 redirect from any domain (for example: www, dev, w3) of your website to direct access via the non-sub-domain url. Nginx makes it easy to do.

Just make your own wildcard server_name directive and add this to your server{} block:

if ($host != '' ) {
  rewrite  ^/(.*)$$1  permanent;