You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Dmitriy Gorbenko <ba...@agenstvo.com> on 2007/05/21 20:39:37 UTC

[users@httpd] Experience in mod_rewrite in Apache 2.2

Hello everyone,

Recently I begin learn capabilities of mod_rewrite, and found that task, 
which I want to resolv, did not resolvs with help of mod_rewrite (or I 
wrongly thinking, and I want to consult).

So, I have Router host, which stand on border between my local network 
an Internet. Also, at my local network where are two resources by the 
point of view of Router host:

http://internal_network_host/first.html
http://internal_network_host/third.html

Well, I want to reproduce next two scripts:

1) If Router gets request from Internet like
    http://router.com/first
he should proxy that request to access 
http://internal_network_host/first.html resource. So, it's look like the 
user from Internet accessed http://internal_network_host/first.html 
directly.

2) If Router gets request from Internet like,
    http://router.com/second
he sould redirect client (responce 302) to http://router.com/third, 
where client could access via proxy mechanism (like first case) resource 
http://internal_network_host/third.html. So:

client: http://router.com/second
router: 302 http://router.com/third
client: http://router.com/third
and router return content of http://internal_network_host/third.html 
resource.


To reproduce such schema via standart RewriteRule sets - it's, I think, 
easy.
But I want do it using external program, like this:

RewriteEngine on
RewriteLog      "/var/www/html/rewrite.log"
RewriteLogLevel 255
RewriteLock     /var/www/html/redir.lock
RewriteMap      rd      prg:/var/www/html/redirect.pl
RewriteRule     ^(.+)$ ${rd:%{THE_REQUEST}}


and source code of external program, like this:

-----------------------8<---------------------------------------------
#!/usr/bin/perl
$| = 1;
while (<STDIN>) {
        $request = $_;
        if ($request eq "GET http://router.com/first HTTP/1.1" ) {
                proxy to access to resource 
http://internal_network_host/first.html
        } elsif ($request eq "GET http://router.com/second HTTP/1.1" ) {
                redirecting to http://router.com/third
        } else {
                do something else....
        }
}
-----------------------8<---------------------------------------------

And now, I did not understand how, by using only one
RewriteRule     ^(.+)$ ${rd:%{THE_REQUEST}}
rule and one programs, I can achieve such goal.

I tell, why I need only this one rule, which grabs all requests and 
calls one external program for every request: because I will apply such 
scheme to virtual domains like:
[0-9a-z]*.router.com
and I some domains I will just proxing, and another domains I will 
redirect to another resource. All this should be done by external 
program, and that why I want to aks a help.

Thanks for helping me.
Dmitriy.

Re: [users@httpd] Experience in mod_rewrite in Apache 2.2

Posted by Joshua Slive <jo...@slive.ca>.
On 5/23/07, Dmitriy Gorbenko <ba...@agenstvo.com> wrote:

> Yes, but in this case the logic of making decision (doing proxy or
> redirect) imposes oneself to URL. It still is possible to agree with this.
>
> Let's admit, the user is sent URL, in which where is a word proxy.
> However that logic, which I wish to create, should in some cases do not
> proxy request, but do redirect. And if we have written rule
> RewriteRule ^proxy:(.*)$ $1 [P]
> then the program which process request cannot change action proxy to
> redirect.

Seems we have a serious language barrier here, since I find these two
paragraphs almost incomprehensible due to grammar problems. The
client/server transcripts don't help, because they don't show what
your particular problem is. What exactly about my suggested solution
does not work?

Note that my suggestion didn't require the client to specify proxy: in
the URL. It is your perl rewritemap that puts that in the URL for the
cases it wants to proxy.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Experience in mod_rewrite in Apache 2.2

Posted by Dmitriy Gorbenko <ba...@agenstvo.com>.
Joshua Slive wrote:
> On 5/22/07, Dmitriy Gorbenko <ba...@agenstvo.com> wrote:
>
>> I think I could do it using mod_rewrite, but now I understand, that I
>> can't: in apache config file I can create rules, which will makes an
>> decision: redirect or proxy request of client (R and P flags).
>> But by using external program I can't make such decision.
>> And that is a problem.
>
> Ahhh, so your question is: how do I activate rewrite flags from a prg:
> RewriteMap. (Not at all clear from your first email.)
>
> Indeed, this can't be done directly, but it can be easily done
> indirectly. For example, have your map program prefix the URL with
> proxy: or redirect: depending on what you want, and then:
> RewriteRule ^(.+)$ ${rd:%{THE_REQUEST}}
> RewriteRule ^proxy:(.*)$ $1 [P]
> RewriteRule ^redirect:(.*)$ $1 [R]
>
> (Use the RewriteLog to debug!)
>
> Joshua.
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server
> Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> " from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>

Yes, but in this case the logic of making decision (doing proxy or
redirect) imposes oneself to URL. It still is possible to agree with this.

Let's admit, the user is sent URL, in which where is a word proxy.
However that logic, which I wish to create, should in some cases do not
proxy request, but do redirect. And if we have written rule
RewriteRule ^proxy:(.*)$ $1 [P]
then the program which process request cannot change action proxy to
redirect.

Look at this three examples - they perfectly illustrate that behaviour
which I wish to create:

C:  GET http://ofiice.domain.com/document.html
S:  302 - http://login.domain.com/ (redirect method)
C:  GET http://login.domain.com/?username=aaa&passwd=bbb <http://login.domain.com/?username=aaa&passwd=bbb>
S:  302 - http://office.domain.com/afjgn39nxnx8n3f29ss/document.html
(redirect method)
C:  GET http://office.domain.com/afjgn39nxnx8n3f29ss/document.html
S:  gives resource content (proxy to some local resource)

Clients tries to acces some document, but server want client to be
authenticated before. Server create paramert session-id
(afjgn39nxnx8n3f29ss), and client should use this session-id to access
resource office.domain.com. And later, than client send this session-id.
server will proxy such request to local network server, which is not
public, and which contains 'document.html' resource.

Or another example, where client, 1 hour later, tries to access the same
document:

C:  GET http://office.domain.com/afjgn39nxnx8n3f29ss/document.html
S:  302 - http://login.domain.com/ (redirect method)
C:  GET http://login.domain.com/?username=aaa&passwd=bbb <http://login.domain.com/?username=aaa&passwd=bbb>
S:  302 - http://office.domain.com/jh43hkjshdfsdyhskdvc/document.html
(redirect method)
C:  GET http://office.domain.com/jh43hkjshdfsdyhskdvc/document.html
S:  gives resource content (proxy to some local resource)
And another one (here session-id parametr is correct, and we just doing proxing):

C:  GET http://office.domain.com/ndfklweuixxnlfljdklj/document.html <http://office.domain.com/jh43hkjshdfsdyhskdvc/document.html>
S:  gives resource content (proxy to some local resource)



Re: [users@httpd] Experience in mod_rewrite in Apache 2.2

Posted by Joshua Slive <jo...@slive.ca>.
On 5/22/07, Dmitriy Gorbenko <ba...@agenstvo.com> wrote:

> I think I could do it using mod_rewrite, but now I understand, that I
> can't: in apache config file I can create rules, which will makes an
> decision: redirect or proxy request of client (R and P flags).
> But by using external program I can't make such decision.
> And that is a problem.

Ahhh, so your question is: how do I activate rewrite flags from a prg:
RewriteMap. (Not at all clear from your first email.)

Indeed, this can't be done directly, but it can be easily done
indirectly. For example, have your map program prefix the URL with
proxy: or redirect: depending on what you want, and then:
RewriteRule ^(.+)$ ${rd:%{THE_REQUEST}}
RewriteRule ^proxy:(.*)$ $1 [P]
RewriteRule ^redirect:(.*)$ $1 [R]

(Use the RewriteLog to debug!)

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org


Re: [users@httpd] Experience in mod_rewrite in Apache 2.2

Posted by Dmitriy Gorbenko <ba...@agenstvo.com>.
Joshua Slive wrote:
> On 5/21/07, Dmitriy Gorbenko <ba...@agenstvo.com> wrote:
>
>> And now, I did not understand how, by using only one
>> RewriteRule     ^(.+)$ ${rd:%{THE_REQUEST}}
>> rule and one programs, I can achieve such goal.
>
> I can't personally tell what question you are asking here. What
> exactly are you having problems with? Do you want help writing the
> external map program?
>
> As general advice, I'd avoid the external map program if possible.
> They tend to be complicated, fragile, and error-prone. Plus they are
> serialized, so they could become a bottleneck for your site.
>
> I bet you could accomplish the same thing use a dbm RewriteMap.
>
> Joshua.
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server
> Project.
> See <URL:http://httpd.apache.org/userslist.html> for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>   "   from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>

Well, I'll try to explain: let's look at fictitious session between
server and client:

C:  GET http://ofiice.domain.com/document.html
S:  302 - http://login.domain.com/ (redirect method)
C:  GET http://login.domain.com/?username=aaa&passwd=bbb
S:  302 - http://office.domain.com/afjgn39nxnx8n3f29ss/document.html
(redirect method)
C:  GET http://office.domain.com/afjgn39nxnx8n3f29ss/document.html
S:  gives resource content (proxy method)

Clients tries to acces some document, but server want client to be
authenticated before. Server create some session-id
(afjgn39nxnx8n3f29ss), and client should use this session-id to access
resource office.domain.com. And later, than client send this session-id.
server will proxy such request to local network server, which is not
public, and which contains 'document.html' resource.

Or another example, where client, 1 hour later, tries to access the same
document:

C:  GET http://office.domain.com/afjgn39nxnx8n3f29ss/document.html
S:  302 - http://login.domain.com/ (redirect method)
C:  GET http://login.domain.com/?username=aaa&passwd=bbb
S:  302 - http://office.domain.com/jh43hkjshdfsdyhskdvc/document.html
(redirect method)
C:  GET http://office.domain.com/jh43hkjshdfsdyhskdvc/document.html
S:  gives resource content (proxy method)

I think I could do it using mod_rewrite, but now I understand, that I
can't: in apache config file I can create rules, which will makes an
decision: redirect or proxy request of client (R and P flags).
But by using external program I can't make such decision.
And that is a problem.



Re: [users@httpd] Experience in mod_rewrite in Apache 2.2

Posted by Joshua Slive <jo...@slive.ca>.
On 5/21/07, Dmitriy Gorbenko <ba...@agenstvo.com> wrote:

> And now, I did not understand how, by using only one
> RewriteRule     ^(.+)$ ${rd:%{THE_REQUEST}}
> rule and one programs, I can achieve such goal.

I can't personally tell what question you are asking here. What
exactly are you having problems with? Do you want help writing the
external map program?

As general advice, I'd avoid the external map program if possible.
They tend to be complicated, fragile, and error-prone. Plus they are
serialized, so they could become a bottleneck for your site.

I bet you could accomplish the same thing use a dbm RewriteMap.

Joshua.

---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
   "   from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org