【发布时间】:2019-01-15 17:56:40
【问题描述】:
我今天尝试了很多事情,但我没有获胜。我的站点中有一个文件是偶然创建的,其中包含一个特殊字符。因此,Googlebot 已停止抓取 3 周,并且网站管理员工具/搜索控制台不断通知我并希望重新测试该网址。
我想要实现的只是配置 Nginx 以匹配以下请求并将它们重定向到正确的位置,但正则表达式让我难倒这个。
未编码的 URL 字符串为:
/historical-rainfall-trends-south-africa-1921–2015.pdf
编码后的 URL 字符串为:
/historical-rainfall-trends-south-africa-1921%C3%A2%E2%82%AC%E2%80%9C2015.pdf
如何获得这些位置匹配?
更新:
仍然失去理智,我尝试过的任何方法都不起作用。 我在这里得到了这个正则表达式的匹配 - https://regex101.com/r/3Lk2zr/3
然后使用这个
location ~ /.*[^\x00-\x7F]+.* {
return 444;
}
仍然给我一个 404 而不是 444
同样,我得到了一个匹配 - https://regex101.com/r/80KWJ8/1 但后来
location ~ /.*([^?]*)\%(.*)$ {
return 444;
}
给出 404 而不是 444 ??????
也试过了,但还是不行。来源:https://serverfault.com/questions/656096/rewriting-ascii-percent-encoded-locations-to-their-utf-8-encoded-equivalent
location ~* (*UTF8).*([^?]*)\%(.*)$ {
return 444;
}
location ~* (*UTF8).*[^\x00-\x7F]+.* {
return 444;
}
临时解决方案
感谢@funilrys 和How do I redirect all requests that contains a certain string to 404 in nginx?
现在 100% 有效
location /resources {
expires 3h;
add_header Cache-Control 'must-revalidate, proxy-revalidate, max-age=10800';
location ~* \.(jpg|jpeg|png|gif|ico|css|js)$ {
expires 3h;
add_header Cache-Control 'must-revalidate, proxy-revalidate, max-age=10800';
}
location ~* \.(pdf)$ {
expires 30d;
add_header Cache-Control 'must-revalidate, proxy-revalidate, max-age=2592000';
if ($request_uri ~ .*%.*) { return 301 https://example.com/resources/weather-documents/historical-rainfall-trends-south-africa_1921_2015.pdf; }
if ($request_uri ~ .*[^\x00-\x7F]+.*) { return 301 https://example.com/resources/weather-documents/historical-rainfall-trends-south-africa_1921_2015.pdf; }
}
【问题讨论】:
标签: regex nginx nginx-location