1

我想编写一个重写器来搜索与带有或不带有 www 的域名匹配的子文件夹中的 robots.txt 和 sitemap.xml 文件。举个例子:

  • 我有域名 aaa.com、bbb.com 和 ccc.com
  • 它们都安装在同一个根文件夹 %{DOCUMENT_ROOT}
  • 可以使用或不使用 www 访问它们。

如果有人试图访问http://aaa.com/robots.txt文件,我想执行以下操作:

如果请求的文件是 robots.txt {

  • 如果有与子文件夹 %{DOCUMENT_ROOT}/aaa.com/robots.txt 匹配的文件(给出此文件并停止)*1
  • 否则如果有文件与子文件夹 %{DOCUMENT_ROOT}/www.aaa.com/robots.txt 匹配(给这个文件并停止)*2
  • 否则给文件 %{DOCUMENT_ROOT}/robots.txt *3

}

我不想硬编码域名;我试图从请求中获取它们,但我无法检查条件 *2 和 *3:

RewriteCond %{REQUEST_URI} ^/robots.txt$
RewriteRule ^robots\.txt$ /www\.%{HTTP_HOST}/robots\.txt [L]
RewriteCond %{REQUEST_URI} ^/sitemap.xml$
RewriteRule ^sitemap\.xml$ /www\.%{HTTP_HOST}/sitemap\.xml [L]

感谢您的帮助 !

4

1 回答 1

0

尝试:

# prevent any kind of looping:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

# first check host/robots.txt
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/%2/%{REQUEST_URI} -f
RewriteRule ^(robots\.txt|sitemap\.xml)$ /%2/$1 [L]

# then check www.host/robots.txt
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteCond %{DOCUMENT_ROOT}/www.%2/%{REQUEST_URI} -f
RewriteRule ^(robots\.txt|sitemap\.xml)$ /www.%2/$1 [L]

# finally, do nothing and allow the "/robots.txt" request to resolve itself
于 2013-11-15T00:49:32.983 回答