Today we will guide you through the process of creating temporary and permanent redirects with Nginx web server.
What is URL Redirect?
URL Redirect or also known as URL Forwarding, is a technique to redirect a domain name or an URL from a location to another. The common use for this redirection is when a page on your site has a good SEO ranking, and you need to change it for a specific reason. For example, you are switching from a CMS to another, this will make your URL structure changed. So, in order to keep your existing SEO rank of a page, you can redirect/forward the old URL to the new ones.
Difference Between 301 and 302 Redirect
301 Redirect
HTTP 301 status code means that a page has been permanently moved to another location. For example, you have a page at http://domainone.com/contactus/ and you want to redirect it to the newly developed one at http://domainone.com/contact-us/. If you want to keep your SEO ranking, you need to make 301 redirect. If you used 302 redirect, search engines will keep the old page indexed, and think your new page as duplication.
302 Redirect
HTTP 302 status code means that a page has been temporarily moved to another location. For example, you have a contact page at http://domainone.com/contact-us/ and you are redesigning it. To avoid your visitors from seeing the unfinished page, you can make a 302 redirection to another page, or even to the main page.
In this guide, we will discuss and show you how to create temporary and permanent redirects with nginx.
Redirect page
Place the following code into your nginx server block to permanently redirect https://www.domainone.com/how-to-create-redirection.html to https://www.domainone.com/how-to-create-redirection-with-nginx.html
if ( $request_filename ~ how-to-create-redirection.html ) { rewrite ^ https://www.domainone.com/how-to-create-redirection-with-nginx.html permanent; }
If you want to make it temporary, simply replace “permanent” with “redirect”
Now, if you have a list of URLs to redirect, it is better to use a map. Please note that the map definition should be placed ourside of the server block.
map $request_uri $new_uri { include redirections.conf; } server { listen 80; hostname domainone.com www.domainone.com; rewrite ^ http://domaintwo.com$request_uri? permanent; }
In the redirections.conf file, you can write something like:
/old-url /new-url; /contactus /contact-us; /old.html /new.html;
Also, don’t for get to end each lina with a character “;”
Redirect domain
For example, you want to redirect your domainone.com to domaintwo.com, we can do this in nginx configuration file:
server { listen 80; hostname domainone.com www.domainone.com; rewrite ^ http://domaintwo.com$request_uri? permanent; }
As you can see the above line tell nginx to permanently redirect the http://domainone.com and http://www.domainone.com to http://domaintwo.com. If you want to make it temporary, simply replace the “permanent” part in rewrite line with “redirect”
Or, you can also do the redirection using the following configuration:
server { listen 80; hostname domainone.com www.domainone.com; return 301 http://domaintwo.com$request_uri; }
Please note that the two options are correct, but “return 301” directive is better than “rewrite”, it also faster. For more information about “return” and “rewrite” directives, please check nginx official documentation at https://www.nginx.com/resources/wiki/start/topics/tutorials/config_pitfalls/?highlight=rewrite#taxing-rewrites
Next, if you want to redirect all http traffic to https, you can do the following:
server { listen 80; hostname domainone.com www.domainone.com; return 301 https://domainone.com$request_uri; }
With the above configuration, your http version of your domainone.com and www.domainone.com will be permanently redirected to https://domainone.com
When a visitor type https://www.yourdomainone.com on his web browser, your server will response to it and serve the website to the visitor. It means your server is serving https in both www and non www version of your domain. This is bad for SEO, you will want to choose the final URL, with www or without www.
We need to define a separate server block for the www version.
server { listen 443 ssl; server_name www.domainone.com; return 301 https://example.com$request_uri; }
The full configuration would be:
server { listen 80; hostname domainone.com www.domainone.com; return 301 https://domainone.com$request_uri; } server { listen 443 ssl; server_name www.domainone.com; return 301 https://example.com$request_uri; } server { listen 443 ssl; server_name domainone.com; root /var/www/domainone.com; index index.html index.php index.nginx-debian.html; ssl_certificate /etc/letsencrypt/live/domainone.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/domainone.com/privkey.pem; location / { try_files $uri $uri/ index.php; } [......] [......] [......] }
Do not forget to issue “nginx -t” command after editing your nginx configuration files, then restart the service.
With completing this tutorial, you should know how to create temporary and permanent redirects with Nginx.
Of course, you don’t need to get troubled with your server if you are one of our server support services client – in which case, our technical support team will help you in creating temporary and permanent redirects with Nginx. They are available 24/7, and can cater to any questions or requests.
PS. If you liked this post, feel free to share it with your friends by using the social media share shortcuts below, or simply leave a comment. Thanks.