用于用连字符替换空格/空白并将URL转换为小写的Nginx重写规则
如何将所有URL转换为小写,并在NGINX中将空格用于用连字符替换空格/空白并将URL转换为小写的Nginx重写规则,nginx,url-rewriting,url-rewrite-module,Nginx,Url Rewriting,Url Rewrite Module,如何将所有URL转换为小写,并在NGINX中将空格“替换为-连字符???我搜索了一些东西,发现perl脚本可以帮助我们解决这一问题。所以我在这里分享一个解决方案。该解决方案的可行性如何,或者它是一种最佳实践,也许NGINX专家可以对此有所了解 首先在nginx.conf中,在http块中添加以下perl脚本 # Include the perl module perl_modules perl/lib; # Define function perl_set $uri_lower
“
替换为-
连字符???我搜索了一些东西,发现perl脚本可以帮助我们解决这一问题。所以我在这里分享一个解决方案。该解决方案的可行性如何,或者它是一种最佳实践,也许NGINX专家可以对此有所了解
首先在nginx.conf
中,在http块中添加以下perl脚本
# 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
z中,是因为我需要在多个vhost中使用此函数
现在在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;
}
如果有其他人能指导我找到更好的方法,我很乐意应用。
希望有帮助。我搜索了一些东西,发现perl脚本可以在这方面帮助我们。所以我在这里分享一个解决方案。该解决方案的可行性如何,或者它是一种最佳实践,也许NGINX专家可以对此有所了解
首先在nginx.conf
中,在http块中添加以下perl脚本
# 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
z中,是因为我需要在多个vhost中使用此函数
现在在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;
}
如果有其他人能指导我找到更好的方法,我很乐意应用。
希望有帮助。我搜索了一些东西,发现perl脚本可以在这方面帮助我们。所以我在这里分享一个解决方案。该解决方案的可行性如何,或者它是一种最佳实践,也许NGINX专家可以对此有所了解
首先在nginx.conf
中,在http块中添加以下perl脚本
# 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
z中,是因为我需要在多个vhost中使用此函数
现在在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;
}
如果有其他人能指导我找到更好的方法,我很乐意应用。
希望有帮助。我搜索了一些东西,发现perl脚本可以在这方面帮助我们。所以我在这里分享一个解决方案。该解决方案的可行性如何,或者它是一种最佳实践,也许NGINX专家可以对此有所了解
首先在nginx.conf
中,在http块中添加以下perl脚本
# 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
z中,是因为我需要在多个vhost中使用此函数
现在在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;
}
如果有其他人能指导我找到更好的方法,我很乐意应用。
希望能有帮助