我想编写一个rewriterule来搜索子文件夹中的robots.txt和sitemap.xml文件,这些文件与包含或不包含www的域名匹配。我们来举个例子:
如果有人试图访问http://aaa.com/robots.txt文件,我想执行以下操作:
如果要求的文件是robots.txt {
}
我不想对域名进行硬编码;我试图从请求中取出它们,但我无法检查条件* 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]
感谢您的帮助!
答案 0 :(得分: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