How to Set Redirection Rules in NGINX Webserver?

How to Set Redirection Rules in NGINX Webserver?

The rewrite rules are used to redirect one URL to another URL or to invoke an internal proxy fetch. Most of the people who are using shared hosting, they used to configure rewrite rules in Apache’s .htaccess file and they also try to use that redirect rule in nginx web server.

To something like this:

server {

listen       80;

server_name  www.example.org  example.org;

RewriteCond %{HTTPS} off

RewriteRule ^(.*)$ Error! Hyperlink reference not valid. [L,R=301]

}

 

This is wrong and ineffective. In this tutorial, we will discuss how to create rewrite rules in NGINX. Now I introduce some common redirection rule in NGINX like HTTP to HTTPS, old domain to new domain etc.

In NGINX webserver, it needs to edit the current configuration file to set redirection. First, make a backup of the current config file before doing anything. It’s very important to take a backup because if we do something wrong the whole site will down. You can take a copy of config file using “cp” command.

Assume that my current config file name is “mydomain.com”, use this command to take a backup.

cp mydomain.com mydomain_back.com

Then open your config file with an editor.

vi mydomain.com

 

Redirecting from an old name to new name

server {

listen 80;

listen 443 ssl;

server_name www.mydomain.com mydomain.com;

return 301 $scheme://www.new-mydomain.com$request_uri;

}

 

This sample NGINX rewrite rule permanently redirects requests from www.mydomain.com and mydomain.com to www.new‑mydomain.com, using two NGINX variables to capture values from the original request URL – $scheme is the original protocol (http or https) and $request_uri is the full URL (following the domain name), including arguments. Because $request_uri captures the portion of the URL that follows the domain name, this rewrite is suitable if there’s a one‑to‑one correspondence of pages between the old and new sites (for example, www.new‑mydomain.com/about has the same basic content as www.mydomain.com/about)

 

Adding and removing “www” prefix

ADD www prefix:

# add ‘www’

server {

listen 80;

listen 443 ssl;

server_name mydomain.com;

return 301 $scheme://www.mydomain.com$request_uri;

}

Remove www prefix:

# remove ‘www’

server {

listen 80;

listen 443 ssl;

server_name www.mydomain.com;

return 301 $scheme://mydomain.com$request_uri;

}

 

Redirecting all traffic to the correct domain name

Here’s a special case that redirects incoming traffic to the website’s homepage when the request URL doesn’t match any server and location blocks, perhaps because the domain name is misspelled. It works by combining the default_server parameter to the listen directive and the underscore as the parameter to the server_name directive.

server {

listen 80 default_server;

listen 443 ssl default_server;

server_name _;

return 301 $scheme://www.mydomain.com;

}

 

Redirecting all request forcing to https(SSL/TLS)

This server block forces all visitors to use a secured (SSL/TLS) connection to your site.

server {

listen 80;

server_name www.mydomain.com;

return 301 https://www.mydomain.com$request_uri;

}

Finally, you need to restart “nginx” service to effect these redirections.

service nginx restart

or

/etc/init.d/nginx restart

 

If you need any further help please do reach our support department.