【发布时间】:2017-07-21 20:00:38
【问题描述】:
如何将我的所有 URL 转换为小写并在 NGINX 中将空格 " " 替换为 - 连字符??
【问题讨论】:
标签: nginx url-rewriting url-rewrite-module
如何将我的所有 URL 转换为小写并在 NGINX 中将空格 " " 替换为 - 连字符??
【问题讨论】:
标签: nginx url-rewriting url-rewrite-module
我搜索了一些东西,发现 perl 脚本 可以帮助我们解决这个问题。所以我在这里分享一个解决方案。该解决方案的可行性或最佳实践,也许 NGINX 专家可以对此有所了解。
首先在 nginx.conf 中添加以下 perl 脚本到 http block
# Include the perl module
perl_modules perl/lib;
# Define function
perl_set $uri_lowercase 'sub {
my $r = shift;
my $uri = $r->uri;
$uri = lc($uri); # lowercase conversion
# replace space with - hyphen
my $search = " ";
my $replace = "-";
$uri =~ s/$search/$replace/ig;
return $uri;
}';
我想保留在nginx.conf 中的原因是我需要在多个虚拟主机中使用此功能。
现在在你的 Vhost 文件中写下这些行
# In case you want your static content's URL should not be converted to lowercase
# Rewrite skip check jpg uppercase characters. leave it blank no processing is required.
location ~ [A-Z]*\.(jpg|jpeg|gif|png|bmp|ico|flv|swf|css|js) {
}
# now check for uppercase and convert it into lowercase
location ~ [A-Z] {
rewrite ^(.*)$ $scheme://$host$uri_lowercase;
}
# Finally check the whitepaces and replace them
location ~ [\s+] {
rewrite ^(.*)$ $scheme://$host$uri_lowercase;
}
如果其他人可以指导我采用更好的方法,我将很乐意应用它。 希望对您有所帮助。
【讨论】: