Nginx 重写(小写和子字符串)
Nginx Rewrite(lowercase and substring)
全部,
我是 nginx 和服务器的新手,我在处理 nginx 重写规则时遇到了麻烦。我想将所有网址小写并删除一些字符串。
我有一个电子商务网站,当前的 URL 结构是这样的:(为了避免被 Whosebug 识别为链接,我在 http 和 : 之间故意添加了一个 space)
product page: http ://www.example.com/Some-Product-pd123456.html
product category: http ://www.example.com/Product-Cat-pl12345.html
page a: http ://www.example.com/About-Us.html
page b: http ://www.example.com/Contactus.html
那么,我想要实现的是这些:
product page: https ://www.example.com/product/some-product/
product category: https ://www.example.com/category/product-cat/
page a: https ://www.example.com/about-us/
page b: https ://www.example.com/contact-us/
注意,我的网站将从 http 转到 https,并且我有多个页面,如页面 a(相同模式)和多个页面,如页面 b(无模式)。到目前为止,这些是我所做的:
//product
location ~ ^http:\/\/www\.example\.com\/(.*)-pd\d{6}\.html$ {
return 301 https://www.example.com/product//;
}
//product category
location ~ ^http:\/\/www\.example\.com\/(.*)-pl\d{5}\.html$ {
return 301 https://www.example.com/category//;
}
//Page a
location ~ ^http:\/\/www\.example\.com\/(.*)\.html$ {
return 301 https://www.example.com//;
}
//Page b
location ~ ^http:\/\/www\.example\.com\/Contactus\.html$ {
return 301 https://www.example.com/contact-us/;
}
所以我的问题是如何先将 url 小写,然后在它们正确的情况下应用上述规则,或者将它们更好、更有效地组合在一起,以获得更好的网站速度?位置顺序重要吗?感谢您的帮助。
我在这里回答我自己的问题。
为了小写 url,一种方法是使用 perl。
查看此答案:
然后,以产品页面为例,这是我在服务器块中的代码:
location ~* "/(.*)-pd\d{6}\.html$" {
return 301 http://www.example.com/product//;
}
注意,如果你的正则表达式中有{},你需要在外面加上双引号。
全部, 我是 nginx 和服务器的新手,我在处理 nginx 重写规则时遇到了麻烦。我想将所有网址小写并删除一些字符串。 我有一个电子商务网站,当前的 URL 结构是这样的:(为了避免被 Whosebug 识别为链接,我在 http 和 : 之间故意添加了一个 space)
product page: http ://www.example.com/Some-Product-pd123456.html
product category: http ://www.example.com/Product-Cat-pl12345.html
page a: http ://www.example.com/About-Us.html
page b: http ://www.example.com/Contactus.html
那么,我想要实现的是这些:
product page: https ://www.example.com/product/some-product/
product category: https ://www.example.com/category/product-cat/
page a: https ://www.example.com/about-us/
page b: https ://www.example.com/contact-us/
注意,我的网站将从 http 转到 https,并且我有多个页面,如页面 a(相同模式)和多个页面,如页面 b(无模式)。到目前为止,这些是我所做的:
//product
location ~ ^http:\/\/www\.example\.com\/(.*)-pd\d{6}\.html$ {
return 301 https://www.example.com/product//;
}
//product category
location ~ ^http:\/\/www\.example\.com\/(.*)-pl\d{5}\.html$ {
return 301 https://www.example.com/category//;
}
//Page a
location ~ ^http:\/\/www\.example\.com\/(.*)\.html$ {
return 301 https://www.example.com//;
}
//Page b
location ~ ^http:\/\/www\.example\.com\/Contactus\.html$ {
return 301 https://www.example.com/contact-us/;
}
所以我的问题是如何先将 url 小写,然后在它们正确的情况下应用上述规则,或者将它们更好、更有效地组合在一起,以获得更好的网站速度?位置顺序重要吗?感谢您的帮助。
我在这里回答我自己的问题。
为了小写 url,一种方法是使用 perl。
查看此答案:
然后,以产品页面为例,这是我在服务器块中的代码:
location ~* "/(.*)-pd\d{6}\.html$" {
return 301 http://www.example.com/product//;
}
注意,如果你的正则表达式中有{},你需要在外面加上双引号。