Skip to main content

When you type in a URL, you’re essentially sending a request to a server. And just like you, that server can get confused about where it’s supposed to redirect you. If you’ve ever arrived at an error page, it’s likely because the server didn’t know how to handle your request.

The same thing can happen when you try to access a website that has been moved or is no longer active. Fortunately, there’s a way to ensure that your visitors always end up in the right place: by using Nginx to fulfill 301 redirects to the root domain of https.

What is a 301 redirect?

When you type a URL into your web browser, the first thing that happens is a request is sent to a DNS, or Domain Name System. This system is basically a phone book for the internet; it takes the domain name you typed in (like and translates it into an IP address that can be understood by computers (like Once your computer has the IP address of the server hosting, it sends a request to that server for the page you want to view.

However, sometimes the DNS doesn’t have the correct IP address for a domain name, or the server might be down. In these cases, you might see a “404 error” message telling you that the page you’re looking for can’t be found.

A 301 redirect is a way to resolve these errors by telling the DNS where to find the correct IP address for a domain name. When you type in, the DNS will send your request to the server specified in the 301 redirect instead of trying to look up the domain name itself. This ensures that you always end up at the right place, even if something goes wrong with the DNS lookup process.

Difference between 301 and 302

Difference between 301 and 302

So what’s the difference between these two codes? Well, a 301 redirect indicates that a page has been permanently moved to a new location, while a 302 redirect indicates that the move is only temporary.

Now I want search engine treat the original url as non-existence anymore, and completely shift to the new address, so 301 will be used

Redirect Http to Https

The simplest method is to return a redirected address with a 301 status code in the middle, otherwise the default 302 will be assumed.

server {
  listen 80;
  return 301 https://$host$request_uri;

Return and Rewrite are both Nginx rewrite directives; however, because the path does not need to be altered here, return will be more convenient.

$host and $request uri are both embedded variables in the Nginx http module; combining these two parameters is comparable to removing the http:// from the request url.

www Redirect to Root Domain

Because all http has been redirected to https, we only need to handle in https.

server {
  listen 443 ssl;
  server_name ~^(?<www>www\.)?(.+)$;
  if ( $www ) {
    return 301 https://$2$request_uri;

Regular Expression matching the server name was used, which may be enabled by appending ~ in front of its value, and PCRE grammar is supported.

Regex is used to determine if there is a www prefix and to capture the root domain, which will yield two variables, one for the name capture variable $www and the other for the numeric capture variable $2.

numeric capture variables are not supported in if, or else there will be error thrown (unknown “1” variable), that’s why ?<www> is added to assign $1 to $www

Reduce Redirect Times

The aforementioned settings produce the desired result, but there is a flaw: will first redirect to, then back to Redirecting twice is clearly inferior than redirecting once, therefore one-stop would be preferable, and the http config will be updated as follows:

server {
  listen 80;
  server_name ~^(?:www\.)?(.+)$;
  return 301 https://$1$request_uri;

In the corresponding http server, server name is also set to regex mode, and $host is replaced with the captured root domain $1.

Www will be removed directly, so capturing is not required; instead, use the ?: symbol to complete grouping without capturing, so the following root domain becomes $1.

As a result, whether or not there is a www, it will be forwarded to a https domain without a www.


There you have it! Now you know how to use Nginx to fulfill 301 redirects to the root domain of HTTPS. As we’ve seen, Nginx is more efficient than other options like Apache, making it the best choice for high-traffic sites that need to perform a lot of 301 redirects.