请帮助我理解子域 robots.txt 的简单 nginx 重写问题

发布于 2024-10-28 01:52:11 字数 579 浏览 2 评论 0原文

对于我的子域,我想指向不同的 robots.txt 文件。我希望以下代码能够正常工作:

if ($host ~ subdomain) {
    rewrite ^/favicon.ico$ /favicon.ico break;
    rewrite ^/robots.txt$ /robots.subdomain.txt break;
    rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2&$query_string last;
}

favicon.ico 工作正常,所有其他扩展都可以正常重写为index.php,但robots.txt 也是如此。

我花了[浪费]很多时间试图解决这个问题,我是通过在 robots.txt 重写后添加以下行来解决这个问题的。

    rewrite ^/robots.subdomain.txt$ /robots.subdomain.txt break;

有人可以帮助我为什么它只在我添加此行时才起作用,如果您发现任何明显的低效率,对我的配置的任何改进也将受到欢迎!谢谢。

For my subdomain I wanted to point to a different robots.txt file. I had hoped the following code would work:

if ($host ~ subdomain) {
    rewrite ^/favicon.ico$ /favicon.ico break;
    rewrite ^/robots.txt$ /robots.subdomain.txt break;
    rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2&$query_string last;
}

favicon.ico works fine, all other extensions are rewritten to index.php just fine, but so is robots.txt.

I spent [wasted] a lot of time trying to solve it, which I did by adding the following line after the robots.txt rewrite.

    rewrite ^/robots.subdomain.txt$ /robots.subdomain.txt break;

Can someone please help me why it only works when I add this line, also any improvements to my config would be welcomed if you see any obvious inefficiencies! Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无畏 2024-11-04 01:52:11

这应该就是您要寻找的内容:

location / {
  rewrite ^/robots.txt$ /robots.$host.txt; # rewrites robots.txt

  try_files $uri $uri/ @php; # try_files serves statics and sends everything else
                             # to your front controller (index.php)
                             # files such as favicon.ico get served normally
}

location @php {
 rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2 last;
}

唯一需要注意的是您的 robots.txt 需要以完整主机命名,因此在上面的示例中,您的 www.domain.com 需要有一个 robots.www。文档根目录中的domain.com.txt 文件。这看起来有点难看,所以我会这样做:

rewrite ^/robots.txt$ /$host.robots.txt;

然后将文件命名为 www.example.com.robots.txt

This should be what you're looking for:

location / {
  rewrite ^/robots.txt$ /robots.$host.txt; # rewrites robots.txt

  try_files $uri $uri/ @php; # try_files serves statics and sends everything else
                             # to your front controller (index.php)
                             # files such as favicon.ico get served normally
}

location @php {
 rewrite ^/([^\/]*)?/?(.*)?$ /index.php?in1=$1&in2=$2 last;
}

The only caveat is that your robots.txt needs to be named after the full host, so in the example above your www.domain.com needs to have a robots.www.domain.com.txt file in the document root. This seems a bit ugly, so I'd do it this way instead:

rewrite ^/robots.txt$ /$host.robots.txt;

and then you name your file www.example.com.robots.txt

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文