Skip to content

Instantly share code, notes, and snippets.

@mariocesar
Last active February 18, 2020 14:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariocesar/8e1ef0bd620105e9654cc0471c4da5b5 to your computer and use it in GitHub Desktop.
Save mariocesar/8e1ef0bd620105e9654cc0471c4da5b5 to your computer and use it in GitHub Desktop.
NGINX / Rewrite all Uppercase urls to the lowercase form
# Testing with docker:
# docker run --rm -it -p 8000:80 -v $(pwd)/nginx.conf:/etc/nginx/nginx.conf nginx:stable-perl
load_module modules/ngx_http_perl_module.so;
user nginx;
worker_processes 1;
http {
include /etc/nginx/mime.types;
# Load perl module library from nginx
perl_modules perl/lib;
# Define a perl function
perl_set $rewrite_to_lowercase '
sub {
# shift will get the first argument of the functio, that will be the request
my $r = shift;
# Extract just the uri
my $uri = $r->uri;
# lc converts to lower case LowerCase
$uri = lc($uri);
return $uri;
}
';
server {
listen 80 default_server;
location ~ [A-Z] {
set $new_request_uri $rewrite_to_lowercase;
return 301 $scheme://$http_host$new_request_uri$is_args$args;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment