case insensitive rewrite in nginx

How to do Case Insensitive Rewrite in NGINX

NGINX allows you to rewrite requested URLs in numerous ways. In most cases it does a case sensitive rewrite of URLs. But sometimes you may need to do case insensitive rewrite in NGINX. In this article, we will learn how to do this.


How to do Case Insensitive Rewrite in NGINX

Let us say you want to rewrite URL http://example.com/product to http://example.com/new-product. Typically, here is the rewrite statement we use in our NGINX server configuration to implement the above redirect.

rewrite "product" http://www.example.com/new-product redirect;
OR
location /foobar {
     rewrite "product" http://www.example.com/new-product redirect;
}

The above configuration will redirect URLs starting with /product but not those with /PRODUCT or any different case of /product.

In case you want to do a case insensitive match of URLs, you can just add (?i) before capture group as shown below.

rewrite "(?i)product" http://www.example.com/new-product redirect;
OR
location /foobar {
     rewrite "(?i)product" http://www.example.com/new-product redirect;
}

The above configuration should help you match your required capture group irrespective of the case of URL.

In this article, we have learnt how to do case sensitive rewrite in NGINX.

Also read:

How to Forward Request to Another Port in NGINX
How to Check if Key Exists in Python Dictionary
How to Get Random Number Between Two Numbers in JS
NGINX Catch All Location
How to Download Images in Python

Leave a Reply

Your email address will not be published.