大网数据广告活动
网站运营与故障问答

Can the files robots.txt and sitemap.xml be dynamic through an .htaccess redirect?

· 52CE 我爱测

I have a multilanguage and multidomain site. It runs through a unique CMS installation (Drupal), so I have a single root directory. So if I have a static robots.txt, there I only can show the files for a single domain, as far as …

问题原文

原帖链接 · Cesar · CC BY-SA 3.0

I have a multilanguage and multidomain site. It runs through a unique CMS installation (Drupal), so I have a single root directory. So if I have a static robots.txt, there I only can show the files for a single domain, as far as I know.

Could I put a line in .htaccess

Redirect 301 /robots.txt /robots.php

(or equivalent instruction, and please, indicate which one if allowed)

so it redirects to a dynamic php file, where I can serve different contain according to the $_SERVER['HTTP_HOST']?

And the same question for sitemap.xml, so I can serve a dynamic sitemap.php which indicates different links for each different domain.

The problem with no using .txt and .xml is, as mentioned, that all the domains share a single physical directory in the server computer.

已采纳解答

原帖链接 · Stephen Ostermiller · CC BY-SA 3.0

You can make any file dynamic. The best way to do so is not through redirects, but through rewrite rules.

RewriteRule ^robots\.txt$  /robots.php [L]

That way, you power it with a dynamic script, but the URL doesn't change. Most crawlers (including Googlebot) will follow redirects for robots.txt, but some crawlers will get confused if you introduce redirects.

Note that even if you power it with PHP, your robots.txt should appear to be static to each crawler for each domain. It is fine to serve different content for different domains, or even for different user agents. However, serving different content randomly, or based on time of day can really confuse search engine crawlers and mess up your SEO.


Sitemaps are fine to name however you want. You could redirect those, or use a rewrite rule to power them dynamically at the same URL. You can also name them like

  • site-a-sitemap.xml
  • site-b-sitemap.xml
  • site-c-sitemap.xml

Then refer to them in robots.txt:

Sitemap: http://www.example.com/example-sitemap.xml

or submit them to the search engines manually through their webmaster tools or search console.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2015-10-22)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。