如何让Apache有/robots.txt导致文件,无论域名?

时间:2015-07-26 18:42:32

标签: apache apache2 robots.txt

我使用以下网址运行本地服务器:

foo.self
bar.self
blah-blah.self

以上网址由以下VirtualHost声明处理:

<VirtualHost *:80>
    UseCanonicalName Off
    ServerName self
    ServerAlias *.self
    VirtualDocumentRoot C:\Users\Foo\PhpstormProjects\%-2
   <Directory C:\Users\Foo\PhpstormProjects\*>
      Options Indexes FollowSymLinks Includes ExecCGI MultiViews
      Order deny,allow
      Allow from all
      Require all granted
  </Directory>
</VirtualHost>

每个人都有自己的/robots.txt,但我需要做的是让任何网址都返回完全相同的内容,无论他们的/robots.txt包含什么,或者即使它不存在。例如,以下网址:

  • foo.self / robots.txt的
  • bar.self / robots.txt的
  • 嗒嗒-blah.self / robots.txt的

...将返回相同的文字:

User-agent: *
Disallow: /

这不依赖于301 RedirectRewriteRule

1 个答案:

答案 0 :(得分:5)

只需在主机配置中为/robots.txt创建指向同一文件的别名即可。另外,可能需要Location指令才能授予访问权限:

<VirtualHost *:80>
    UseCanonicalName Off
    ServerName self
    ServerAlias *.self

    Alias /robots.txt C:\Somfolder\robots.txt
    <Location "C:\Somfolder\robots.txt">
      Order deny,allow
      Allow from all
    </Location>

    VirtualDocumentRoot C:\Users\Foo\PhpstormProjects\%-2
   <Directory C:\Users\Foo\PhpstormProjects\*>
      Options Indexes FollowSymLinks Includes ExecCGI MultiViews
      Order deny,allow
      Allow from all
      Require all granted
  </Directory>
</VirtualHost>

您将此添加到所有主机配置,所有这些Alias指令都指向同一个文件。文件C:\Somfolder\robots.txt是&#34;正常&#34;你描述的robots.txt文件。

您可以通过将该指令包含为模式来简化此操作。这样您就可以将指令放在单独的配置文件中,并且只将include指令添加到主机配置中:

<VirtualHost *:80>
    UseCanonicalName Off
    ServerName self
    ServerAlias *.self

    Include C:\path\to\file\robots.inc

    VirtualDocumentRoot C:\Users\Foo\PhpstormProjects\%-2
   <Directory C:\Users\Foo\PhpstormProjects\*>
      Options Indexes FollowSymLinks Includes ExecCGI MultiViews
      Order deny,allow
      Allow from all
      Require all granted
  </Directory>
</VirtualHost>

档案C:\path\to\file\robots.inc

Alias /robots.txt C:\Somfolder\robots.txt
<Location "C:\Somfolder\robots.txt">
  Order deny,allow
  Allow from all
</Location>

请注意,我几乎不了解MS-Windows系统。所以我记下的示例路径可能没有意义。但你应该能够理解: - )