How to redirect to a different domain using NGINX?

前端 未结 8 836
旧时难觅i
旧时难觅i 2020-12-02 07:10

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

相关标签:
8条回答
  • 2020-12-02 07:49

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

    server { 
    
        if ($host = mydomain.com) {
            return 301 http://www.adifferentdomain.com;
        } 
    }
    
    0 讨论(0)
  • 2020-12-02 07:57
    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

    0 讨论(0)
  • 2020-12-02 08:00

    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;
    
    }
    
    0 讨论(0)
  • 2020-12-02 08:05

    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; 
    }
    
    0 讨论(0)
  • 2020-12-02 08:08

    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;
    }
    
    0 讨论(0)
  • 2020-12-02 08:08

    I'm using this code for my sites

    server {
            listen 80;
            listen 443;
            server_name  .domain.com;
    
            return 301 $scheme://newdomain.com$request_uri;
    }
    
    0 讨论(0)
提交回复
热议问题