rewrite vs redirect nginx

Rewrite vs Redirect NGINX

When a user requests a URL, NGINX allows you to serve content from a different URL, using URL rewrite as well as using redirection. 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.

Also read : How to Create Custom 404 page in NGINX


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.

Also read : How to Limit Download Speed in NGINX


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.

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.

Ubiq makes it easy to visualize data, and monitor them in real-time dashboards. Try Ubiq for free.