【问题标题】:Please help me understand simple nginx rewrite issue for subdomain robots.txt请帮助我了解子域 robots.txt 的简单 nginx 重写问题
【发布时间】:2011-04-01 21:36:27
【问题描述】:

对于我的子域,我想指向一个不同的 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;

有人可以帮我解释为什么它只在我添加这一行时才有效,如果你发现任何明显的低效率,我也欢迎对我的配置进行任何改进!谢谢。

【问题讨论】:

    标签: nginx subdomain rewrite robots.txt


    【解决方案1】:

    这应该是你要找的:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-17
      • 2012-05-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 2022-12-17
      相关资源
      最近更新 更多