In this tutorial, we will discuss how to write a redirect configuration on Nginx, and also give you some examples that might help you understand it. There is some purpose why you want to redirect an incoming request:
- Force user to use HTTPS instead of HTTP
- Force user to use www domain instead of root domain or vice versa
- Force users to use visit other domains because you change your domain name, or for other reasons.
Let’s see more detail.
Redirection Type
There are 3XX
series HTTP codes that are related to redirection, but mostly you will use 2 of them, which are 301
and 302
.
301
is used when you want the redirection to be permanent. Quoted from Mozilla.org, they said that the search engines will update their link to the resource, so they will not hit the original URL anymore. rig302
us used when you want the redirection to be temporary. Quoted from Mozilla.org, they said that the search engines will not update their link to the resource, which mean search engine will always hit the original URL and get the302
redirect message each time.
Nginx Redirect Syntax
You can use the return
directive combined with the HTTP code to redirect the incoming request like this
return 3XX url;
3XX
is the HTTP code whether it is 301
or 302
.
url
is the URL target of the redirection.
Usually return
is used within the server
block like this
server {
. . .
return 302 www.example.com;
}
Redirect Examples
To make sure you can use this directive, we will provide you some examples of how to use a redirect in some use cases. let’s check this out.
Redirect HTTP to HTTPS
Redirecting HTTP to HTTPS is a very common use case, it used to force the user to use SSL/TLS to access our website, hence improving security. This is the configuration example
server {
listen 80;
server_name example.com;
return 301 https://example.com$request_uri;
}
This server configuration will listen on port 80 (the default port for HTTP), and then permanently redirect it to the HTTPS version of the domain.
$request_uri
is used to keep the URI including argument if available, so when it is redirected, the URI will not missing.
Redirect Root Domain to WWW Domain or Vice Versa
Another use case is to redirect a root domain to www domain. for example: facsiaginsa.com
redirected to www.facsiaginsa.com
. I personally don’t like to use the www domain, but if you need it, this is the nginx configuration
server {
listen 80;
server_name example.com;
return 301 http://www.example.com$request_uri;
}
if you want the opposite (redirect www domain to root domain), you can use this configuration instead
server {
listen 80;
server_name www.example.com;
return 301 http://example.com$request_uri;
}
Redirect Current Domain to Other Domain
The last use case is redirecting your domain to another domain. It is used when you want to change your domain name. So you want to drive the traffic from the previous domain to the new one. This is the configuration example:
server {
listen 80;
listen 443 ssl;
server_name example.com;
ssl_certificate /path/tp/cert.crt;
ssl_certificate_key /path/to/certkey.key;
return 301 $scheme://otherexample.com$request_uri;
}
In this use case, we listen to both http
and https
and then we will redirect the request accordingly. $scheme
contains the protocol values from the original request URL (http
or https
), so the http
request will be redirected to http
, and https
will be redirected to https
.
That’s it! Now you understand how to create a redirect in Nginx.