Mobile app version of vmapp.org
Login or Join
Michele947

: Mod_Rewrite Error code: ERR_TOO_MANY_REDIRECTS As stated in the title, I encounter a redirect loop when accessing my pages in the website. Here is my rewrite config. RewriteCond %{HTTP_REFERER}

@Michele947

Posted in: #Apache2 #ModRewrite

As stated in the title, I encounter a redirect loop when accessing my pages in the website.

Here is my rewrite config.

RewriteCond %{HTTP_REFERER} ^http://example.com/test/store.do [NC]
RewriteRule (.*)$ /test/ [R]


So, how do I solve this redirect error?

Here is the log file

[Fri May 16 12:18:20.345204 2014] [rewrite:trace3] [pid 29462:tid 1193511232] mod_rewrite.c(475): [client 172.16.28.96:60187] 172.16.28.96 - - [example.com/sid#1e84d1c0][rid#1e954910/initial] applying pattern '(.*)$' to uri '/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test/ /store.do', referer: example.com/test/store.do [Fri May 16 12:18:20.345213 2014] [rewrite:trace4] [pid 29462:tid 1193511232] mod_rewrite.c(475): [client 172.16.28.96:60187] 172.16.28.96 - - [example.com/sid#1e84d1c0][rid#1e954910/initial] RewriteCond: input='http://example.com/test/store.do' pattern='^http://example.com/test/store.do' [NC] => matched, referer: example.com/test/store.do [Fri May 16 12:18:20.345218 2014] [rewrite:trace2] [pid 29462:tid 1193511232] mod_rewrite.c(475): [client 172.16.28.96:60187] 172.16.28.96 - - [example.com/sid#1e84d1c0][rid#1e954910/initial] rewrite '/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//whatshot_v3/hahah/store.do' -> '/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//store.do', referer: example.com/test/store.do [Fri May 16 12:18:20.345233 2014] [rewrite:trace2] [pid 29462:tid 1193511232] mod_rewrite.c(475): [client 172.16.28.96:60187] 172.16.28.96 - - [example.com/sid#1e84d1c0][rid#1e954910/initial] explicitly forcing redirect with example.com/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test/ /store.do, referer: example.com/test/store.do [Fri May 16 12:18:20.345239 2014] [rewrite:trace1] [pid 29462:tid 1193511232] mod_rewrite.c(475): [client 172.16.28.96:60187] 172.16.28.96 - - [example.com/sid#1e84d1c0][rid#1e954910/initial] escaping example.com/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//store.do for redirect, referer: example.com/test/store.do [Fri May 16 12:18:20.345245 2014] [rewrite:trace1] [pid 29462:tid 1193511232] mod_rewrite.c(475): [client 172.16.28.96:60187] 172.16.28.96 - - [example.com/sid#1e84d1c0][rid#1e954910/initial] redirect to example.com/test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//test//store/store.do [REDIRECT/302], referer: example.com/test/store.do



UPDATE:

I have found the solution for my problem.

RewriteCond %{HTTP_REFERER} ^http://example.com/test/store.do [NC]
RewriteRule ^/test - [R,L] #prevent for any URL to be place under /test.Thus , prevent looping.
RewriteCond %{HTTP_REFERER} ^http://example.com/test/store.do [NC]
RewriteRule ^/(.*)$ /test/ [R,L]

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Michele947

2 Comments

Sorted by latest first Latest Oldest Best

 

@Eichhorn148

I am doing reverse proxy, so when a user enters example.com/test. I want it to jump to example.com/test/store.do.


If this is your problem, then your rewrite rule should be very simple:

RewriteRule /test/?$ /test/store.do [R]


You really shouldn't be using the referrer in you rewrite rule unless you want to block change what people see based on where they came from. For example, show an error message when people come from some a specific other website.

Here are the reasons that your rewrite rule was failing:


You were saying: "If somebody come from /test/store.do, add an extra /test to the beginning of their current page and redirect.
The browser would add the extra test and redirect, but redirects don't change the referrer. It would send the same referrer again, which would trigger the same rule.
You would keep adding /test and redirecting until you get too many redirects.


I'm not sure exactly how being behind a reverse proxy is figuring in to your problem. You should have two sets of URLs. One that is user visible, and one that is backend only. Something like:


Front end: example.com/test/store.do Back end: 127.0.0.1:8080/commercepackage/2012/store.do

You don't say which of these hosts is having the rewrite rule on it. If it is on the back end, your rewrite rules should use the path on the backend (/commercepackage/2012), not the path the users see (/test).

10% popularity Vote Up Vote Down


 

@Sherry384

It has been nearly 12 years since I have done any proxy work so I am at a disadvantage. At one point proxies were something I knew cold and configured daily, but tonight, I am just not getting my head around this. As well, I am very adept at doing extremely weird things using .htaccess but not so much for the normal stuff that people do. However, I do see some errors that I will address and throw in some other thoughts that might help.

I invite people to jump in and correct anything I may have made a mistake on. Please.

Your rule creates a loop. Your RewriteRule created the exact condition that the RewriteCond is designed to trap. Each time a rewrite is made, in effect, the entire validation process is started over only to be trapped again and so on. You need an out.

As well, you are using the variable without filling the variable using () in the RewriteCond. If you were trying to fill this in the RewriteRule, then you are appending the original request to /test/ which I do not think you want.

Just addressing these few things...

RewriteCond %{HTTP_REFERER} !^http://example.com/test/$ [NC]
RewriteCond %{HTTP_REFERER} ^http://example.com/test/store.do$ [NC]
RewriteRule .* /test/ [R,L]


The first line is your out. It basically says, if I see the condition I just created, then forget it- all is okay.

I treated this as a standard rewrite, however, while I use HTTP_REFERER for about 60% of my work, I am not sure it applies here. You may be the best judge. Referrer is not the request. It is the from location and not the to location. In other words, it is the page the person has left and not the page the person is going to. Here is another option to try.

RewriteCond %{REQUEST_URI} !^/test/$ [NC]
RewriteCond %{REQUEST_URI} ^/test/store.do$ [NC]
RewriteRule .* /test/ [R,L]


I mentioned {REQUEST_FILENAME} in my comments, but I am not sure this is what you want, but on the off chance you want to experiment, it refers to the store.do. You may also noticed that I book-ended the regular expressions with both ^ and $. While this is not always necessary, it is generally safer to do.

Let me know if I was able to help. I can always take another SWAG at it.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme