nginx 不区分大小写
Case insensitivity in nginx
我们有几个 SEO 页面,例如:
http://www.example.com/PageOne.html
我们在配置中重定向如下:
location = /PageOne.html {
rewrite ^/(.*) /seo.php?id=1 last;
}
问题是如果用户通过键入访问此页面:
http://www.example.com/pageone.html
"Page Not Found" 显示错误。大约有 500 多个 seo 页面。如何为 nginx 编写规则以忽略 url 中的区分大小写?我想要一个适用于所有 url.
的通用解决方案
专门针对 PageOne.html
,您可以执行以下操作:
location ~ /PageOne.html {
return 301 http://www.example.com/pageone.html;
}
如果您有多个需要重定向的 URI,这似乎是最佳选择 is to use Perl:
location ~ [A-Z] {
perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}
如果您有数百个独特的 URI,其中涉及许多 location
块,我会考虑更改您的应用程序以处理小写 URI,而不是期望网络服务器处理小写转换。
这解决了我的问题。遗憾的是,与这些问题相关的文章并不多,甚至 nginx 也没有提供用户友好的 Help/Tutorials.
location ~* ^/-PageOne.html {
rewrite ^ /seo.php?page_id=1 last;
}
希望对您有所帮助!
我们有几个 SEO 页面,例如:
http://www.example.com/PageOne.html
我们在配置中重定向如下:
location = /PageOne.html {
rewrite ^/(.*) /seo.php?id=1 last;
}
问题是如果用户通过键入访问此页面:
http://www.example.com/pageone.html
"Page Not Found" 显示错误。大约有 500 多个 seo 页面。如何为 nginx 编写规则以忽略 url 中的区分大小写?我想要一个适用于所有 url.
的通用解决方案专门针对 PageOne.html
,您可以执行以下操作:
location ~ /PageOne.html {
return 301 http://www.example.com/pageone.html;
}
如果您有多个需要重定向的 URI,这似乎是最佳选择 is to use Perl:
location ~ [A-Z] {
perl 'sub { my $r = shift; $r->internal_redirect(lc($r->uri)); }';
}
如果您有数百个独特的 URI,其中涉及许多 location
块,我会考虑更改您的应用程序以处理小写 URI,而不是期望网络服务器处理小写转换。
这解决了我的问题。遗憾的是,与这些问题相关的文章并不多,甚至 nginx 也没有提供用户友好的 Help/Tutorials.
location ~* ^/-PageOne.html {
rewrite ^ /seo.php?page_id=1 last;
}
希望对您有所帮助!