今天我已经尝试了很多事情,但我只是没有获胜。我的网站中有一个文件是偶然创建的,其中包含一个特殊字符。结果,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
我怎样才能获得这些的位置匹配?
UPDATE:
我仍然失去了理智,我所尝试的一切都不起作用。
我在这里得到了与这个正则表达式的匹配 -https://regex101.com/r/3Lk2zr/3 https://regex101.com/r/3Lk2zr/3
但然后用这个
location ~ /.*[^\x00-\x7F]+.* {
return 444;
}
仍然给我一个 404 而不是 444
同样,我得到了与此的匹配 -https://regex101.com/r/80KWJ8/1 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 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 以及这个如何在nginx中将所有包含特定字符串的请求重定向到404? https://stackoverflow.com/questions/17221408/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; }
}