如何在 nginx 中重定向一些 url?
How to redirect a few urls in nginx?
我收到了一些用户的请求,希望让某些页面在搜索引擎中无法被发现。虽然使用 robots.txt
是个好主意,但我想使用 NGINX 将此类页面重定向到解释这种情况下发生的情况的页面。
本质上,这就是我想要的:
http://example.com/some/url.pdf --> http://example.com/redirected_due_to_privacy
http://example.com/another_url --> http://example.com/redirected_due_to_privacy
http://example.com/and/another --> http://example.com/redirected_due_to_privacy
我希望所有其他 URL 都指向自身(保持不变)。
执行此操作的最佳方法是什么?
我认为您可以为每个页面创建不同的 url,并在 nginx.conf 的位置模块中添加重写规则。
如:
重写“^/redirected_due_to_privacy$”/some/url.pdf 中断;
重写“^/redirected_due_to_privacy1$”/another_url;
重写“^/redirected_due_to_privacy2$”/and/another
map, rather than rewrite, return 或重定向,在这种情况下很有用:
map $uri $privacyredir {
default 0;
/some/url.pdf 1;
/another_url 1;
/and/another 1;
}
...
server {
...
# putting this inside a location is be more efficient
if ($privacyredir) {
return 301 $scheme://$host/redirected_due_to_privacy;
}
}
我收到了一些用户的请求,希望让某些页面在搜索引擎中无法被发现。虽然使用 robots.txt
是个好主意,但我想使用 NGINX 将此类页面重定向到解释这种情况下发生的情况的页面。
本质上,这就是我想要的:
http://example.com/some/url.pdf --> http://example.com/redirected_due_to_privacy http://example.com/another_url --> http://example.com/redirected_due_to_privacy http://example.com/and/another --> http://example.com/redirected_due_to_privacy
我希望所有其他 URL 都指向自身(保持不变)。 执行此操作的最佳方法是什么?
我认为您可以为每个页面创建不同的 url,并在 nginx.conf 的位置模块中添加重写规则。
如:
重写“^/redirected_due_to_privacy$”/some/url.pdf 中断;
重写“^/redirected_due_to_privacy1$”/another_url;
重写“^/redirected_due_to_privacy2$”/and/another
map, rather than rewrite, return 或重定向,在这种情况下很有用:
map $uri $privacyredir {
default 0;
/some/url.pdf 1;
/another_url 1;
/and/another 1;
}
...
server {
...
# putting this inside a location is be more efficient
if ($privacyredir) {
return 301 $scheme://$host/redirected_due_to_privacy;
}
}