1

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)
Bobík
  • 111
  • 3
  • Have you checked [the canonical answer](http://serverfault.com/q/214512/184397)? – zagrimsan Sep 21 '15 at 12:23
  • Hi, not this one, but may simmilar articles, including official docs. Do you see some obvious mistake in my conf? – Bobík Sep 22 '15 at 22:05
  • I'm no expert in rewrite engine, but isn't the last rule just telling the system to do what you describe it does? "Take the REQUEST_URI and append it to "/build/sub/folder": Thus, when your REQUEST_URI is "/folder", the rewritten result is "/build/sub/folder/folder". As that seems to not be what you want, you should describe better the intended result in your question. – zagrimsan Sep 23 '15 at 06:53
  • Yes, it propably does what I described. :-) I just don't know where I described it. It redirects to correct file, but I problem is that it redirects, not rewrites. (I am adding more information to the question.) – Bobík Sep 24 '15 at 04:53
  • Are you sure the rewrite rules in the question are really the rules that you have in the Apache config right now? I'm asking since the rules say that if there is to be redirection from www.example.com -> example.com the second rule should not be evaluated. http://htaccess.madewithlove.be/ agrees with that, but your quoted test results don't. – zagrimsan Sep 25 '15 at 07:35
  • @zagrimsan Good point. I edited question to contain whole .htaccess file. (I just swapped domains to example.com.) – Bobík Oct 07 '15 at 23:33
  • I still find your example conflicting with the rule set provided, and also the verbal description of the issue has some oddities. You say `http://example.com/folder` results 301 redirect, and looking at the rules a request without `www.` 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 there `www.example.com` as 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
  • I am on shared hosting, so I can not see httpd.conf. You are telling .htaccess seems OK and it should not behave like this, so I will try to solve it with support. Thank you. – Bobík Nov 11 '15 at 02:23

0 Answers0