Jess Williams
Jess Williams

Reputation: 23

Case-insensitive URL in nginx

I want to be able to access both www.mrmerch.com/TERMS and www.mrmerch.com/terms. I currently have two subfolders set up to accomplish this - TERMS and terms. Inefficient and incorrect, I know.

Here is my current configuration. I added the very last location block, but I still can't hit anything that doesn't exactly match that (e.g., "Terms"). Any help is greatly appreciated. Thanks!

server   {
server_name www.mrmerch.com;
return 301 $scheme://mrmerch.com$request_uri;
}

server {
listen 80 default_server;
server_name mrmerch.com;
server_tokens off;

access_log /var/log/nginx/mrmerch.access_log;
error_log /var/log/nginx/mrmerch.error_log;

root /home/paul/www/mistermerch.com/;
try_files $uri $uri/ /index.php$is_args$args;

gzip on;
gzip_comp_level 6;
gzip_vary on;
gzip_min_length  1000;
gzip_proxied any;
gzip_types text/plain text/html text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6].";

location = /favicon.ico {
            log_not_found off;
            access_log off;
    }

    location = /robots.txt {
            allow all;
            log_not_found off;
            access_log off;
    }

location /artwork {
    alias /home/mistermerch/www/;
}

location ~ \.php$ {
    try_files   $uri =404;
    fastcgi_pass   127.0.0.1:9000;
    fastcgi_index  index.php;
    fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
    #fastcgi_param  SCRIPT_FILENAME /home/paul/www/mistermerch.com$fastcgi_script_name;
    include fastcgi_params;
}

location ~* \.(js|css|png|jpg|jpeg|gif|ico)$ {
            expires max;
            log_not_found off;
    }


    location ~* ^/Terms/ {
    }



}

Upvotes: 1

Views: 5997

Answers (1)

Richard Smith
Richard Smith

Reputation: 49812

You could use a case insensitive regex location to rewrite the URI internally:

location ~* ^/terms {
    rewrite ^ /static/terms.html last;
}

In this case, /terms, /terms/, /TeRmS, and /tErMs/ etc. are all rewritten to a location which is outside the regex location match.

If you wish to use a destination URI which also matches the regex location, you can extract it with an exact match location:

location ~* ^/terms {
    rewrite ^ /terms/index.html last;
}
location = /terms/index.html {
}

See this document for details.

Upvotes: 2

Related Questions