Rewrite/redirect loop

Jan 19, 2012 at 6:34 PM
Edited Jan 19, 2012 at 6:42 PM

Hello, you've solved all my other questions, maybe you can help me out with this one. I have the subdomain blog.test.com. I was able to make a rewrite from www.test.com/blog. I also want a redirect from blog.test.com to www.test.com/blog just in case someone still access the URL via the subdomain. I used the followiung rule, but it seems to be in a loop! Can you give me a tip on how to solve this?

Thanks,

Nicolas

 

RewriteCond %{HTTP_HOST} ^blog.test.net [NC] 

RedirectRule ^(.*)$ http://www.test.net$1 [R=301]

 

RewriteCond %{HTTP_HOST}   ^www\.test\.net$  [I]

RewriteCond %{HTTP_URL} /blog

ProxyPass ^(.*)$ http://blog.test.net/$1 [U,I,L]

Coordinator
Jan 19, 2012 at 7:30 PM

Here's what's likely happening:

  1. a request arrives for http:// blog.test.net/blog
  2. The first rule fires, which sends an HTTP 301 (redirect) response, referring to http:/ /w ww.test.net/blog
  3. The original requester sends a new request, this time to http:/ /w ww.test.net/blog
  4. The second rule fires, which causes IIRF to send out a proxy request to http:// blog.test.net/blog
  5. The outbound request is received by IIRF, go to step 1.

one way to avoid the loop, is to insert a condition in the first rule that does not cause the redirect when the request is already being proxied.  You can do this by looking for the Via: header, which IIRF adds to all outbound proxy requests as per RFC 2616 (http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html).

A better way to avoid the problem, is to simply not do tricky things with hostnames.  If you truly want to remove blog.test.net from public use, then why not, in the ProxyPass rule, use a unique internal-only hostname, that has never been published?  Instead of proxying to blog.test.net, proxy to blog-internal01.test.net, and change the hostname of the target machine. This would work if the blog.test.net domain is under your control.  If it is not, then I suppose you're doing something tricky on purpose. In which case, good luck.

 

Jan 19, 2012 at 8:40 PM

The truth is I dont have control over the subdomain. So I tried using the http:via command on the first rule:
RewriteCond %{HTTP_HOST} ^blog.test.net [NC]

RewriteCond %{HTTP:VIA} !^192.168.1.3$ [NC] 

RedirectRule ^(.*)$ http://www.test.net/blog$1 [R=301]

 


RewriteCond %{HTTP_HOST}   ^www\.test\.net$  [I]

RewriteCond %{HTTP_URL} /blog

ProxyPass ^(.*)$ http://blog.test.net$1 [U,I]


The loops has ended, but its not redirecting, I'm getting the following. What is "GetServerVariable: cannot find that variable"? If the http:via has the ! and => TRUE, why does the rule apply?


Thu Jan 19 12:32:45 -  1396 - DoRewrites

Thu Jan 19 12:32:45 -  1396 - DoRewrites: Url (decoded): '/

'Thu Jan 19 12:32:45 -  1396 - EvaluateRules: depth=0

Thu Jan 19 12:32:45 -  1396 - EvaluateRules: RewriteBase is empty (root vdir)

Thu Jan 19 12:32:45 -  1396 - EvaluateRules: Rule 1: 2 match

Thu Jan 19 12:32:45 -  1396 - EvaluateRules: Rule 1: evaluating conditionThu Jan 19 12:32:45 -  1396 - ApplyUrlEncoding: out 'blog.test.net'

Thu Jan 19 12:32:45 -  1396 - EvalCondition: Cond t(%{HTTP_HOST}) op(~) p(blog.test.net) => TRUE

Thu Jan 19 12:32:45 -  1396 - GetServerVariable: cannot find that variable

Thu Jan 19 12:32:45 -  1396 - GetServerVariable: cannot find that variableThu Jan 19 12:32:45 -  1396 - ApplyUrlEncoding: out ''

Thu Jan 19 12:32:45 -  1396 - EvalCondition: Cond t(%{HTTP:VIA}) op(~) p(!^192.168.1.3$) => TRUE

Thu Jan 19 12:32:45 -  1396 - EvalConditionList: rule 1, TRUE, Rule will apply

Thu Jan 19 12:32:45 -  1396 - ApplyUrlEncoding: out 'http://www.test.net/blog/'

Thu Jan 19 12:32:45 -  1396 - EvaluateRules: Result (length 38): http://www.test.net/blog/

Thu Jan 19 12:32:45 -  1396 - EvaluateRules: returning 1301

Thu Jan 19 12:32:45 -  1396 - DoRewrites: Redirect (code=301) Url to: 'http://www.test.net/blog/'

Coordinator
Jan 20, 2012 at 7:05 AM

I think your syntax is wrong.  It's not HTTP:VIA, it's HTTP_VIA .

 

Jan 20, 2012 at 1:46 PM

Ok, I'm trying to condition the rule: if HTTP_VIA is not 1* then do the rule. I´m missing something!!!!

RewriteCond %{HTTP_VIA} !^(1.*)$  [NC]

Something is not matching!

Fri Jan 20 05:40:12 -  3460 - EvalCondition: t(1.1 192.168.1.3 (IIRF v2.1)) op(~) p(!^(1.*)$)

Fri Jan 20 05:40:12 -  3460 - EvalCondition: match result: 2 (match)

Fri Jan 20 05:40:12 -  3460 - EvalCondition: Cond t(%{HTTP_VIA}) op(~) p(!^(1.*)$) => FALSE

Fri Jan 20 05:40:12 -  3460 - EvalConditionList: rule 1, FALSE, Rule does not apply

Jan 20, 2012 at 2:41 PM

I'm using RewriteCond %{HTTP_VIA} ^$  [NC] and it seems to be working. I'll let you know!

Thanks

Nicolas