Details the configuration of the Nginx 301 redirection

  • 2020-05-12 07:00:18
  • OfStack

Details the configuration of the Nginx 301 redirection

301 redirects are common requirements, such as accessing nowamagic.net and automatically jumping to www.nowamagic.net. Or the other way around, visit www.nowamagic.net and jump to nowamagic.net. Configuring 301 redirects (301 redirect) in Nginx is easy. Here's how.

Open the nginx.conf file and find your server configuration section:


server {
	listen    80;
	server_name nowamagic.net www.nowamagic.net;
	#server_name localhost;
	if ($host != 'www.nowamagic.net' ) { 
		rewrite ^/(.*)$ http://www.nowamagic.net/$1 permanent; 
	} 
}

If the URL being accessed is an address without www, jump to URL with www. That is, let the domain name without www jump to the domain name with www.

Or a separate server rule for URL without www:


server {
	server_name nowamagic.net;
	rewrite ^(.*) http://www.nowamagic.net$1 permanent;
}

Upload nginx.conf, then nginx-s reload. The 301 configuration for Nginx is simple.

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: