2011-03-31 77 views
0

对于我的子域,我想指向一个不同的robots.txt文件。我希望下面的代码将工作:请帮我理解子域robots.txt的简单nginx重写问题

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; 

是否有人可以帮助我,为什么它只有当我加入这行,如果你还看不到任何明显的低效到我的配置任何改进将受到欢迎的作品!谢谢。

回答

0

这应该是你在找什么:

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