SEO-Friendly Nginx Server 301 Redirect Rules
When multiple domains are bound to one website, you’ll find that each domain shows differently, so redirection is needed. There are two implementation methods. The first method is to check the nginx core variable host (older versions use http_host):
server {server_name www.zhaojian.net zhaojian.net ;if ($host != 'www.zhaojian.net' ) {rewrite ^/(.*)$ http://www.zhaojian.net/$1 permanent;}...}The second method:
server {server_name zhaojian.net;rewrite ^/(.*) http://www.zhaojian.net/$1 permanent;}I use the first method. In both methods, permanent is the key. For detailed explanation, see nginx redirect rules documentation.
last – Basically always use this Flag. break – Stop Rewrite, don’t continue matching redirect – Return HTTP status 302 for temporary redirect permanent – Return HTTP status 301 for permanent redirect
Now you can check the result. You can view the returned HTTP headers here: