首页 > 解决方案 > 防止通过ip地址访问

问题描述

使用 express.js 如何防止请求通过 ip 地址而不是通过 cloudflare 前面的域访问我的服务器?

这是为了防止 googlebot 索引http://xx.xx.xx.xxx/

标签: node.jsexpress

解决方案


这就是我最终的结果,希望它有效。

app.get('/*', function(req, res, next) {
  if (req.host === '129.8d.xx.xxx') {
    res.redirect(301, 'https://example.com' + req.path)
  }
  else {
    next();
  }
})

推荐阅读