使用Apache mod_rewrite将主机重写到子文件夹,但redirect而不是重写

我想通过.htaccess模拟虚拟主机,所以我想重写主机名到子文件夹。 它工作,但在一些URL不重写,但redirect。

这是我的.htacess文件:

RewriteEngine on RewriteBase / # Rewrite from subdomains to subfolders with builds RewriteCond %{HTTP_HOST} ^(.*)\.(.*)\.ci\.example\.com$ RewriteCond %{REQUEST_URI} !^/builds/ RewriteCond %{REQUEST_URI} !^/robots.txt RewriteRule ^(.*)$ /builds/%2/%1/$1 [PT] # Example project RewriteCond %{HTTP_HOST} ^old\.domain\.com$ [NC,OR] RewriteCond %{HTTP_HOST} ^example\.com$ [NC] RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L] # Rewrite production to master subfolder RewriteCond %{HTTP_HOST} ^www\.example\.com$ RewriteCond %{REQUEST_URI} !^/builds/ RewriteRule ^(.*)$ builds/example-project/master/$1 [PT] 

如果我访问例如http://example.com/它将正确地重写到/builds/sum/folder 。 如果http://example.com/page.html将重写为/builds/sum/folder/page.html 。 但是,如果我访问http://example.com/folder它不会重写,但301redirect到/builds/sum/folder/folder (并在那里加载index.html )。

为什么它总是重写,但如果目标path不存在的文件,但redirect到index.html文件夹? (我想把它全部改写,所以浏览器中的URL不会改变。)

编辑:为了更好的理解发生了什么:

 Bobik-MBP:Sites Bobik$ telnet www.example.com 80 Trying 1.2.3.4... Connected to example.com. Escape character is '^]'. GET http://www.example.com/folder <!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN"> <html><head> <title>301 Moved Permanently</title> </head><body> <h1>Moved Permanently</h1> <p>The document has moved <a href="http://www.example.com/builds/sub/folder/">here</a>.</p> </body></html> Connection closed by foreign host. $ telnet www.example.com 80 Trying 1.2.3.4... Connected to example.com. Escape character is '^]'. GET http://www.example.com/folder/index.html <!DOCTYPE html> <html> ... (requested page)