请帮助我理解子域 robots.txt 的简单 nginx 重写问题
对于我的子域,我想指向不同的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该就是您要寻找的内容:
唯一需要注意的是您的 robots.txt 需要以完整主机命名,因此在上面的示例中,您的 www.domain.com 需要有一个
robots.www。文档根目录中的domain.com.txt
文件。这看起来有点难看,所以我会这样做:然后将文件命名为
www.example.com.robots.txt
This should be what you're looking for:
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:and then you name your file
www.example.com.robots.txt