I want to simulate virtual hosts by .htaccess, so I want to rewrite hostname to subfolder. It works, but on some URLs it don't rewrite but redirects.
This is my .htacess file:
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]
If I access for example http://example.com/ it will rewrites correctly to /builds/sum/folder.
If http://example.com/page.html it will rewrites to /builds/sum/folder/page.html.
But if I access http://example.com/folder it will not rewrites but 301 redirects to /builds/sum/folder/folder (and there it loads index.html).
Why it rewrites always but redirects if target path is not existing file, but folder with index.html? (I want to rewrite it all, so URL in browser will not change.)
EDIT: For better understanding what happens:
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)
http://example.com/folderresults 301 redirect, and looking at the rules a request withoutwww.prefix should do so, but in the example you get 301 even when the rule causing 301 redirect is not even needed (as you have therewww.example.comas host name already). Is this the one you're confused about? If it is, I agree, that shouldn't happen as far as I understand the rules. Are you sure there are no other rules elsewhere (under/etc/apache2/)? – zagrimsan Oct 09 '15 at 07:59