Last updated on July 16th, 2024 at 06:26 am
NGINX is a popular web server that makes it easy to serve websites, big or small. Often web administrators and developers need to move pages from one URL to another. NGINX provides a couple of simple ways to do this. When a user requests a URL, NGINX allows you to serve content from a different URL, using URL rewrite as well as using redirection. In such cases, site administrators often wonder which one to use when. However, there is a critical difference between URL rewrite vs redirect that you need to be aware of, before using them. In this article, we will look at the difference between rewrite vs redirect in NGINX.
Rewrite vs Redirect NGINX
Here is the difference between URL Rewrite vs Redirect in NGINX.
What is URL Rewrite
URL Rewrite allows you to rewrite the requested URL as a different URL before being processed by the server. Here is an example of URL rewrite statement in Apache where we rewrite /product-id/1 as /product/1.html
Rewrite ^product-id/([0-9]+)$ /product/$1.html
In the above example, Apache server will accept request for /product-id/1 and serve content at /product/1.html. It will do this for all product URLs such as /product-id/1, /product-id/2, etc.
In this case, although users request /product-id/1 they are served content /product/1.html without any knowledge. This change of URL happens internally in the server and the user will not know about it as the browser URL does not change.
URL rewrites are useful if you want to display content from a different web page or URL without changing the requested URL.
What is URL Redirect
URL Redirect allows you to redirect browser to a different URL and continue processing at that URL. Here is an example of URL Redirect rule in Apache that allows you to redirect from product-id/1 to /product/1.html
Redirect ^product-id/([0-9]+)$ /product/$1.html
In the above example, when users request for /product-id/1, /product-id/2, etc. they are redirected to /product/1, /product/2, etc. respectively.
URL redirect is useful if you have moved or deleted a URL and want to point users and search engines to its new destination. This is frequently used by webmasters and bloggers all over the world.
Difference between URL Rewrite vs Redirect
In case of URL Rewrite, the server does the URL substitution on its own internally, and serves the response from a different URL. The requested URL in browser does not change.
On the other hand, in case of URL Redirect, when a browser requests for a specific URL, the server sends back a 3xx redirect response to the browser, along with the redirect location. Then the browser automatically sends a new request to the redirect location. So the requested URL in browser changes.
Please note, if redirection is disabled on client web browser, then the second request will not be sent and redirection will not happen.
URL rewrites are useful if you want to display beautiful SEO-friendly and intuitive URLs (e.g /product ) but serve content from non-pretty URLs (e.g index.php?category=1&page=3&konid=213). It is also useful if you want to serve requested URL from a different location, without changing the URL. In this case, you don’t want the search engines and users to know that you are serving content from a different location.
URL redirects are useful, if you have moved your page to a new location and want to serve content from this location. In this case, you are letting users and search engines know that your URL has moved. It is useful when you want to move or restructure your website, or consolidate multiple pages into a single page.
URL Rewrite | URL Redirection |
Happens completely on the web server. Client browser does not notice any change. | Both browser and server are involved. Server sends a 3xx response to client browser with redirected URL. Client browser has to send a new request to redirected URL |
No change in URL in web browser | URL in web browser will change to new redirected URL |
To be used if you want to serve request from a different URL without changing requested URL | To be used if you want to indicate that URL has moved temporarily or permanently |
Used to display user friendly links using content from complex URLs | Used to restructure, consolidate or move web pages |
Can rewrite URL based on user-defined conditions | Can redirect URL matching a string or pattern only. Can’t impose many conditions on redirection |
Can be used to block URL access, using conditions | Cannot be used to block URL access. You can only redirect visitors to another URL. |
Conclusion
In this article, we have learnt about URL rewrite and redirection in NGINX. They hold true for all web servers, not just NGINX. This is because it is a general client-server communication mechanism and not something specific to NGINX alone. Depending on your requirement, you can choose to use URL rewrite or redirection.
URL rewrites are rules that tell server from where the response for a given request needs to be served. On the other hand, URL redirection simply tells web client that the requested URL has moved and it needs to resend request to a new URL.
Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.
Also read :
How to Create Custom 404 page in NGINX
How to Limit Download Speed in NGINX
Sreeram Sreenivasan is the Founder of Ubiq. He has helped many Fortune 500 companies in the areas of BI & software development.