Categories
Uncategorized

nginx case insensitive URL

There was a requirement for a website on the server to have a case-insensitive.

There is no simple option in nginx to make it case-insensitive, there are also no nested if statements available.

This isn’t easy to achieve, but the below seems to have done it me (for only 1 URI!)

location ~* ^/TNG/$ {
return 301 https://www.theirdomain.co.uk/TNG/index.php;
}

location ~* ^/TNG$ {
return 301 https://www.theirdomain.co.uk/TNG/index.php;
}

set $BOTH "";

# Does the case-insensitive pattern match?
if ( $request_uri ~* ^/TNG/index.php$ )
{
set $BOTH "${BOTH}1";
}

# Is it not an exact match to the correct case?
if ( $request_uri != /TNG/index.php )
{
set $BOTH "${BOTH}2";
}

# If both true, then redirect to the correct case
if ( $BOTH = 12 )
{
return 301 https://www.theirdomain.co.uk/TNG/index.php;
}