How to Create 301 Redirection on Nginx and Apache

In this article, we will learn how to redirect the URLs or Domain to another address. This can be done by using the HTTP Redirection. The URL redirection is a popular technique to point one domain address to another domain address which we can achieve on Apache and Nginx both.
We might face a situation in which, we have an established web-site and we need to change the domain for the site. Here, we cannot do this by deleting the older domain since this may cause breakage and disappearance of contents on the old domain is possible and no information will be available for the visitors about how to find the new location. It may also cause the loss of traffic from the old domain which can lead to loosing the credibility of the domain that is already established.
It is always recommended to buy a similar site domain which is near and matches to the original domain for demonstration purpose if we think that our original domain is ‘mydomain.com’ and we should buy a domain which is similar like ‘mydomain.net’ or ‘mydomain.org’.
Currently, redirecting of all web traffic to HTTPS (SSL) is replaced instead of standard HTTP. Using redirection, we can divert all the web traffic which comes from http://mydomain.com to https://mydomain.com.
The most used and common methods of URL redirection are 301 (Permanent redirection) and 302 (temporary redirection).
Temporary redirection will inform the browser that the content is located temporarily with another location and it will access the original URL or web page after restoration.
For example, if we are planning for site maintenance and we want to redirect the site to serve the web needs temporarily, we will redirect all the web traffic from your domain to the maintenance page where we can provide the information to the visitors about the maintenance.
Permanent redirection will inform the browser that the website is no longer available with the old URL and this information should be updated and this point to the new URL we published.
For example, if we needed to change the domain or we needed to change the URL permanently from old location to the new location as if we move the contents to the new location forever.
Apache has different modules, where we can achieve the redirection with mod_alias module and mod_rewrite module that can be used to redirect the web extensively.
Apache can be configured with a simple redirect using the “Redirect” directive, the “Redirect” module which is included in the “mod_alias” module, this redirection needs two arguments one is old URL and another is new URL.
The simple configuration file to redirect looks like below code –
<VirtualHost *:80>
   ServerName www.olddomain.com
   Redirect / http://www.newdomain.com
</VirtualHost>
.
<VirtualHost *:80>
   ServerName www.newdomain.com
   . . .
</VirtualHost>
In the above configuration, we instructed apache to redirect the browser to direct the web request coming to olddomain.com to newdomain.com and this redirection is for a single web page, not applies to the entire site.
By default, the redirection established is 302 for temporary redirection and for permanent redirection can be done with the below lines
It can be done in 2 ways –
Redirect 301 /oldlocation http://www.newdomain2.com/newlocation
or
Redirect permanent /oldlocation http://www.newdomain2.com/newlocation
If we needed to redirect more than a single page, we can use the directive “RedirectMatch”, which allows us to specify the folder which matches the patterns with the regular expressions. Which redirects the entire directories instead of a single page.
RedirectMatch ^/images/(.*)$ http://images.example.com/$1
In the above code the RedirectMatch matches the patterns inside the parenthesis and then use the references for the test in the redirect directive using the value ‘$1’, 1 is the first group of test and the subsequent groups are taken from the numbers sequentially.
Redirection on Nginx can we accomplish using many ways, but the most common way to redirect by creating the server block for the content which we likely to redirect.
server {
   listen 800;
   server_name olddomain.com;
   return 301 $scheme://newdomain.com$request_uri;
}
In the above example, we are redirecting the requests coming to olddomain.com to the newdomain.com with the server block.
It will return the status code given in the URL redirection and it will execute the URL substitution.
‘$scheme ‘ variable is used to redirect the original request (i.e., http or https) to the 301 permanent redirection in the newly formatted URL.
A rewrite directive can be used to process a folder redirection to a separate sub-domain or a new domain with the below configuration.
rewrite ^/images/(.*)$ http://docs.domain2.com/$1 redirect;
This above directive should be placed in the server block of Nginx configuration, which will issue a temporary redirection of the requests that come for the images folder to the new domain or the subdomain we specified.
If we change the redirect to permanent at the end of the above line the redirection is done permanently.
rewrite ^/images/(.*)$ http://docs.domain2.com/$1 permanent;
In the above article, we have learned how to redirect your web page where URLs are requested to a new location. We have to use the correct redirection methods of using the redirects optimally to maintain the web page presence. Also, make sure that it works, when modification to the site is necessary.