NGINX is a powerful open-source web server that can be used for various purposes, including URL redirection. URL redirection is the process of forwarding one URL to another. It is a common practice to redirect URLs when you change the URL structure of your website or when you move content from one page to another.
In this article, we will discuss URL redirection in NGINX and provide some examples to help you understand how it works.
Types of URL Redirection
There are two types of URL redirection:
- 301 Redirect: A 301 redirect is a permanent redirection that tells search engines and browsers that the URL has permanently moved to a new location. It is commonly used when you change the URL structure of your website or when you move content from one page to another.
- 302 Redirect: A 302 redirect is a temporary redirection that tells search engines and browsers that the URL has moved temporarily to a new location. It is commonly used when you need to temporarily redirect traffic to a different page.
Redirecting HTTP to HTTPS
One of the most common uses of URL redirection in NGINX is redirecting HTTP traffic to HTTPS. HTTPS is a secure version of the HTTP protocol that encrypts data between the server and the browser. To redirect HTTP traffic to HTTPS, add the following code to your NGINX configuration file:
1 2 3 4 5 | server { listen 80; server_name example.com; return 301 https://example.com$request_uri; } |
This code listens on port 80 and redirects all HTTP traffic to HTTPS by returning a 301 status code.
Redirecting Non-WWW to WWW URLs
If you want to redirect non-www URLs to their www counterparts, add the following code to your NGINX configuration file:
1 2 3 4 5 | server { listen 80; server_name example.com; return 301 $scheme://www.example.com$request_uri; } |
This code listens on port 80 and redirects all non-www URLs to their www counterparts by returning a 301 status code.
Redirecting WWW to Non-WWW URLs
If you want to redirect www URLs to their non-www counterparts, add the following code to your NGINX configuration file:
1 2 3 4 5 | server { listen 80; server_name www.example.com; return 301 $scheme://example.com$request_uri; } |
This code listens on port 80 and redirects all www URLs to their non-www counterparts by returning a 301 status code.
Redirecting a Specific URL to Another URL
If you want to redirect a specific URL to another URL, add the following code to your NGINX configuration file:
1 2 3 | location /old-url { return 301 https://example.com/new-url; } |
This code redirects all traffic from the /old-url to https://example.com/new-url.
Custom 404 Error Page and Redirect
To create a custom 404 error page and redirect, add the following code to your NGINX configuration file:
1 2 3 4 | error_page 404 /404.html; location = /404.html { internal; } |
This code redirects all 404 errors to the custom 404.html page.
Conclusion
URL redirection is a powerful tool that can be used to redirect traffic from one URL to another. In this article, we discussed the two types of URL redirection and provided some examples to help you understand how it works in NGINX. By understanding URL redirection, you can create a better user experience for your visitors and improve the overall performance of your website.