Forum Moderators: phranque

Message Too Old, No Replies

URL Rewrite

the page isnt redirecting properly, getting into infinite loop

         

supex

8:14 pm on Feb 15, 2010 (gmt 0)

10+ Year Member



Hi,

I have following rule

--------------------------------------------------------
RewriteMap urlspm txt:/usr/local/etc/manual_redirects.txt
RewriteCond ${urlspm:%{REQUEST_URI}} ^(/.*)$
RewriteRule ^.*$ %1 [P,L]
--------------------------------------------------------

and in manual_redirects.txt i have following.

--------------------------------------------------------
/Abc-173000-b /ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173
--------------------------------------------------------

I get following error.

in firefox

--------------------------------------------------------

The page isn't redirecting properly

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
* This problem can sometimes be caused by disabling or refusing to accept cookies.
--------------------------------------------------------

in google chrome

--------------------------------------------------------
The webpage at [xyzcom...] has resulted in too many redirects. Clearing your cookies for this site or allowing third-party cookies may fix the problem. If not, it is possibly a server configuration issue and not a problem with your computer.
--------------------------------------------------------

in IE it goes nowhere, i tried for half hour and it shows its processing.

Now, when I change my .txt file to following(for test purpose) it works.
--------------------------------------------------------
/Abc-173000-b /ex/control/category/~category_id=B173
--------------------------------------------------------

I have a feeling that my URL request is getting into infinite loop somehow. But not sure how and where.

I would appreciate the help.

Thank you in advance.

jdMorgan

10:43 pm on Feb 15, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why are you invoking a proxy through-put in your rule using the [P] flag?

Are there any other rules that can rewrite or redirect the *target* URL of this rule?

Jim

supex

2:00 pm on Feb 16, 2010 (gmt 0)

10+ Year Member



Following is my entire file

---------------------------------------------------
RewriteEngine On

RewriteRule /ex/control/main / [R=301,L]
RewriteRule ^/$ [127.0.0.1...] [P,L]

#for newsletters
RewriteRule ^/img/spacer.gif /newsletter/img/spacer.gif [R,L]

RewriteCond %{QUERY_STRING} ^N=
RewriteRule ^/controller.asp(.*) /ex/control/category/~category_id=0 [R=301]

RewriteMap urls txt:/usr/local/etc/ex_301.txt
RewriteCond ${urls:%{REQUEST_URI}} ^(/.*)$
RewriteRule ^.*$ %1? [R=301,L]

RewriteCond ${urls:%{REQUEST_URI}?%{QUERY_STRING}} ^(/.*)$
RewriteRule ^.*$ %1? [R=301,L]

RewriteMap urlsp txt:/usr/local/etc/redirects.txt
RewriteCond ${urlsp:%{REQUEST_URI}} ^(/.*)$
RewriteRule ^.*$ %1 [P,L]

RewriteMap urlspm txt:/usr/local/etc/manual_redirects.txt
RewriteCond ${urlspm:%{REQUEST_URI}} ^(/.*)$
RewriteRule ^.*$ %1 [P,L]
---------------------------------------------------

jdMorgan

4:48 pm on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Again, why are you proxying all of these requests using the [P] flag?

This code is flawed and needs some major improvements, but there's no use starting until my question is answered.

Jim

supex

6:03 pm on Feb 16, 2010 (gmt 0)

10+ Year Member



Hi Jim,

I am new to Apache world and to re-write I am not sure how proxying will affect the URL rewrite.

jdMorgan

10:04 pm on Feb 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well most of your rules above are telling the server to issue a new HTTP request to pass the client's request on to another server --or to itself-- and then to wait for that other server's response and send it back to the client.

This will cut your server performance in half (or worse), and make a big mess of your log files and stats at the very least, since every request is "doubled."

This illustrates the dangers of using server configuration code that you do not understand.

If no-one else answers, I'll stop back in in awhile and review your code with the new understanding that you are not proxying requests to a back-end application server. In the meantime, check out the resources cited in our Apache Forum Charter, and see if you can't take a few steps toward understanding the code that you intend to use to modify your server configuration. Otherwise, you may well be on the path to putting yourself out of business.

Offered with concern as a sincere warning about the dangers of "tinkering" with server config code...

Jim

supex

1:44 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



What I know is we have 3 servers 1 main and 2 backup/load balancing. But the configuration file for the rewrite is only in main server. So probably that might be the reason we are going back and forth on the request since user might be redirected to other load balancing server.

jdMorgan

2:04 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK. Two 'branches' of discussion, here...

First, it appears that the first two redirects countermand each other, creating a loop. Which of the two URL-paths matched by those rules is the one that you want to be used on the Web? In other words, which URL-path would you call the "old" or "wrong" one, and which would you call the "new" or "correct" URL?

Second, if there are any problems with the remaining rules, they won't be clear unless you post a few anonymized example entries from your RewriteMap text file -- It's impossible to discern the function without knowing the form of the map entries and "where they point to."

Jim

supex

2:16 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Hi Jim,

My non-working URL(existing and which i want to use is)
/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173
and I want to rewrite it to /Abc-173000-b (destination URL). This is written in manual_redirects.txt map file.

There is one more map rule - redirects.txt (which is working map file) file in which the rules are written as

/xyz-195000-b /ex/control/category/~category_id=B195

where /xyz-195000-b is the final URL and /ex/control/category/~category_id=B195 is source URL

In my map file - manual_redirects.txt if I write

/ex/control/category/~category_id=B173 instead of
/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173 it works.

For some reason my source URL (/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173) does not work it goes into infinite loop.

I really appreciate your help.

Thank you.

jdMorgan

2:36 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It really sounds to me like you have many (or all) of your source/destination paths reversed.

http://example.com/ex/control/category/~category_id=B173 is a URL and this is one of the URLs that you must link to on your pages.

/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173 is a server filepath pointing to an existing script, and this is the destination filepath for the rewrite (not redirect) needed to make the site function.

So you rewrite client requests for the URL http://example.com/ex/control/category/~category_id=B173 to the filepath /ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173

As an optional step which should be left until the above problem is resolved, you can detect direct client requests for the old/dynamic URL http://example.com/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173 and 301-redirect them to the new/static URL http://example.com/ex/control/category/~category_id=B173
But don't even start on this aspect of the project until the first steps work properly. I have intentionally omitted the techniques for the required "detection of direct client requests" for this reason.

If it isn't clear, mod_rewrite cannot change URLs. Changing URLs can only be accomplished by editing the links in the HTML of your pages or modifying the script that produces those pages, or by applying "output filters" on Apache 2.0 and above. mod_rewrite cannot change URLs, it can only rewrite an incoming URL-request to a different server filepath, or it can redirect the incoming URL-request to a different URL.

Jim

g1smd

2:52 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



He wants to use URLs like example.com/Abc-173000-b I believe.

[edited by: g1smd at 2:54 pm (utc) on Feb 17, 2010]

supex

2:54 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Sorry I am confused.

http://www.example.com/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173
and
http://www.example.com/ex/control/category/~category_id=B173 both are existing URLs and different pages on the site.

I want to Re-write http://www.example.com/ex/control/brandLanding?SEARCH_STRING=Abc&~category_id=B173 to http:/www.example.com/Abc-173000-b

jdMorgan

2:57 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, massively confusing because of all of the variations...

Jim

g1smd

2:58 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The original question wasn't clear and Jim misread part of it.

I understand the question about the rewrite, but to be completely clear, what do you want to do with the *other* URLs (the 'category' URLs)? Are those staying the same, or is there some other rewrite required for those?

supex

3:45 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



on our site brands and categories both are considered as categories only difference is category id for brands starts with B, so B173 is a brand/category id where as 195 is an actual category id.

So on our site we have one page/format for displaying categories and for which the URL rewrite is in a different file which is redirects.txt. Now I need to create URL rewrite for Brand URLs for which i have manual_redirects.txt

I mentioned the other URL - category one to give idea that if i use that URL in my file it works but when I try my actual URL with BrandLanding it goes into infinite loop.

I hope I have cleared the confusion about my question.

Thank you.

jdMorgan

4:31 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, then I think you'll need to replace your "brandLanding" rule with something like this:

RewriteRule !^/ex/control/brandLanding$ ${urlspm:%{REQUEST_URI}} [P,L]

Note that the RewriteCond isn't needed because the rule itself can contain the map reference, and that the output of this rule is excluded by the negative-match from being re-rewritten.

I presume that you have an "Alias" set up to catch the internal requests resulting from this rule, and 'pipe' them through your load-balancing mechanism. If not (and maybe even if so) the rewritemap substitution path should include a protocol plus a hostname -- a domain or a local IP address).

Jim

supex

5:05 pm on Feb 17, 2010 (gmt 0)

10+ Year Member



Just want to confirm that it should be following right?

RewriteMap urlspm txt:/usr/local/etc/manual_redirects.txt
RewriteRule !^/ex/control/brandLanding$ ${urlspm:%{REQUEST_URI}} [P,L]

jdMorgan

10:53 pm on Feb 17, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I cannot be sure, but it seems to me that you'd either want to hard-code the protocol and hostname in the RewriteMap, or hard-code it in the RewriteRule substitution string. Proxy through-puts are normally coded so that the "output" is a complete URL.

As for the code, please don't ask me to repeat myself. We can help, but it's up to you to test this stuff.

Jim

supex

1:50 pm on Feb 18, 2010 (gmt 0)

10+ Year Member



I tried the following

RewriteMap urlspm txt:/usr/local/etc/manual_redirects.txt
RewriteRule !^/ex/control/brandLanding$ ${urlspm:%{REQUEST_URI}} [P,L]

and it is not working, it is redirecting to other page of the site - www.example.com/admin

and if I manually go to www.example.com/admin it goes to www.example.com