What is wrong with this RewriteCond?

What is wrong with this RewriteCond?

am 17.12.2009 22:24:15 von Perl Whore

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !test
RewriteRule /inbox$ /index.php
RewriteRule /compose-mail$ /index.php <--- this works


RewriteCond %{HTTP_COOKIE} test
RewriteRule /compose-mail$ /proxy/ <--- this does not.
RewriteRule /inbox$ http://127.0.0.1:22131/ [P]
RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P]

What is wrong in the above rules? Even cookie "test" is there, it
still uses the first rule and rewrites as index.php

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 17.12.2009 22:37:33 von Eric Covener

On Thu, Dec 17, 2009 at 4:24 PM, Perl Whore wrote:
> RewriteEngine On
> RewriteCond %{HTTP_COOKIE} !test
> RewriteRule /inbox$ /index.php
> RewriteRule /compose-mail$ /index.php =A0<--- this works
>
>
> RewriteCond %{HTTP_COOKIE} test
> RewriteRule /compose-mail$ /proxy/ <--- this does not.
> RewriteRule /inbox$ http://127.0.0.1:22131/ [P]
> RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P]
>
> What is wrong in the above rules? Even cookie "test" is there, it
> still uses the first rule and rewrites as index.php

A sequence of conditions only applies to the 1 rule immediately
following it. Maybe you need the [C] flag or some restructuring?

--=20
Eric Covener
covener@gmail.com

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 17.12.2009 23:25:21 von Emmanuel Bailleul

> -----Message d'origine-----
> De=A0: Eric Covener [mailto:covener@gmail.com]
> Envoyé : jeudi 17 d=E9cembre 2009 22:38
> À : users@httpd.apache.org
> Objet=A0: Re: [users@httpd] What is wrong with this RewriteCond?
>=20
> On Thu, Dec 17, 2009 at 4:24 PM, Perl Whore wrote:
> > RewriteEngine On
> > RewriteCond %{HTTP_COOKIE} !test
> > RewriteRule /inbox$ /index.php
> > RewriteRule /compose-mail$ /index.php =A0<--- this works
> >
> >
> > RewriteCond %{HTTP_COOKIE} test
> > RewriteRule /compose-mail$ /proxy/ <--- this does not.
> > RewriteRule /inbox$ http://127.0.0.1:22131/ [P]
> > RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P]
> >
> > What is wrong in the above rules? Even cookie "test" is there, it
> > still uses the first rule and rewrites as index.php
>=20
> A sequence of conditions only applies to the 1 rule immediately
> following it. Maybe you need the [C] flag or some restructuring?
>=20
> --
> Eric Covener
> covener@gmail.com
>=20
> ------------------------------------------------------------ ---------
> The official User-To-User support forum of the Apache HTTP Server Project=
..
> See 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

Hello,

I guess the rewritecond does not match because a cookie header will rather =
be of the form "cookie=3Dsomething", so "test" alone is not a good candidat=
e.
If you just want to check for its presence, you may use something like :
RewriteCond %{HTTP_COOKIE} !^.*test.*$
(the '.*' before is important as your cookie may be anywhere in a string co=
ntaining multiple cookies)
and if you want to match some content, something like :
RewriteCond %{HTTP_COOKIE} ^.*test=3D.*blah.*$
should do the trick.

Emmanuel

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 06:13:42 von Perl Whore

I tried that and that did not work either. Still the first rule is
applied but not the 2nd one.

All the rules for /inbox work fine though.

On Fri, Dec 18, 2009 at 3:55 AM, Emmanuel Bailleul
wrote:
>> -----Message d'origine-----
>> De=A0: Eric Covener [mailto:covener@gmail.com]
>> Envoyé : jeudi 17 d=E9cembre 2009 22:38
>> À : users@httpd.apache.org
>> Objet=A0: Re: [users@httpd] What is wrong with this RewriteCond?
>>
>> On Thu, Dec 17, 2009 at 4:24 PM, Perl Whore wrote:
>> > RewriteEngine On
>> > RewriteCond %{HTTP_COOKIE} !test
>> > RewriteRule /inbox$ /index.php
>> > RewriteRule /compose-mail$ /index.php =A0<--- this works
>> >
>> >
>> > RewriteCond %{HTTP_COOKIE} test
>> > RewriteRule /compose-mail$ /proxy/ <--- this does not.
>> > RewriteRule /inbox$ http://127.0.0.1:22131/ [P]
>> > RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P]
>> >
>> > What is wrong in the above rules? Even cookie "test" is there, it
>> > still uses the first rule and rewrites as index.php
>>
>> A sequence of conditions only applies to the 1 rule immediately
>> following it. Maybe you need the [C] flag or some restructuring?
>>
>> --
>> Eric Covener
>> covener@gmail.com
>>
>> ------------------------------------------------------------ ---------
>> The official User-To-User support forum of the Apache HTTP Server Projec=
t.
>> See for more info.
>> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
>> =A0 =A0" =A0 from the digest: users-digest-unsubscribe@httpd.apache.org
>> For additional commands, e-mail: users-help@httpd.apache.org
>
> Hello,
>
> I guess the rewritecond does not match because a cookie header will rathe=
r be of the form "cookie=3Dsomething", so "test" alone is not a good candid=
ate.
> If you just want to check for its presence, you may use something like :
> RewriteCond %{HTTP_COOKIE} !^.*test.*$
> (the '.*' before is important as your cookie may be anywhere in a string =
containing multiple cookies)
> and if you want to match some content, something like :
> RewriteCond %{HTTP_COOKIE} ^.*test=3D.*blah.*$
> should do the trick.
>
> Emmanuel
>
> ------------------------------------------------------------ ---------
> The official User-To-User support forum of the Apache HTTP Server Project=
..
> See for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> =A0 " =A0 from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 07:25:34 von Perl Whore

I gave this a try:

RewriteEngine On
RewriteCond %{HTTP_COOKIE} !^.*test=test.*$
RewriteRule /inbox$ /index.php
RewriteCond %{HTTP_COOKIE} !^.*test=test.*$
RewriteRule /compose-mail$ /index.php

RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /inbox$ http://127.0.0.1:22131/ [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /(js/qui.+) http://127.0.0.1:22131/$1 [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /(css/qui.+) http://127.0.0.1:22131/$1 [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /js/soundmanager2-nodebug-jsmin.js
http://127.0.0.1:22131/js/soundmanager2-nodebug-jsmin.js [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /sound/soundmanager2.swf
http://127.0.0.1:22131/sound/soundmanager2.swf [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /images/favicon.png http://127.0.0.1:22131/images/favicon.png [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /(e/.+) http://127.0.0.1:22131/$1 [P]
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /compose-mail$ /proxy/
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule /compose-mail/(.+)$ /proxy/$1

Now when the cookie not there, it rewrites the url /compose-mail as
/index.php (good!), but when the cookie is not there, it forwards to
/compose-mail as /compose-mail/ and I get a 404 error

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 09:31:18 von Krist van Besien

Enable RewriteLog, with a high RewriteLogLevel. That way you see in
great detail exactly what is happening.

You'll see what matches are tried, against what strings, what matches
are successfull, what rules are considered. Etc...

BTW, but this is advanced stuff, there is a trick to make whole blocks
of RewriteRules depend on one Condition.

1) You can "Chain" rules together. That way a whole block is
evaluated, or skipped based on wether the first one matches. This is
quite handy with a dummy rule...
(Note how the last rule in your block doesn't have the "C" Flag, and
your first rule must be a "dummy" one that always matches, or your
RewriteCond never gets evaluated.)

# First block, executed when cookie not set.
RewriteCond %{HTTP_COOKIE} !^.*test=test.*$
RewriteRule -* - [C]
RewriteRule /inbox$ /index.php [C]
RewriteRule /compose-mail$ /index.php

# Second block, executed when cookie set.
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule -* - [C]
RewriteRule /inbox$ http://127.0.0.1:22131/ [P,C]
RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /(js/qui.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /(css/qui.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /js/soundmanager2-nodebug-jsmin.js
http://127.0.0.1:22131/js/soundmanager2-nodebug-jsmin.js [P,C]
RewriteRule /sound/soundmanager2.swf
http://127.0.0.1:22131/sound/soundmanager2.swf [P,C]
RewriteRule /images/favicon.png http://127.0.0.1:22131/images/favicon.png [P,C]
RewriteRule /(e/.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /compose-mail$ /proxy/ [C]
RewriteRule /compose-mail/(.+)$ /proxy/$1

2) You can use the "S=x" flag, which tells the RewriteEngine to skip
the next x rules, togethet with a dummy rule. Like this:

# Skip the next two rules when the cookie matches.
RewriteCond %{HTTP_COOKIE} ^.*test=test.*$
RewriteRule .* - [S=3]

# These rules are executed when the cookie doesn't match.

RewriteRule /inbox$ /index.php [C]
RewriteRule /compose-mail$ /index.php
RewriteRule .* - [S=10]

# These rules are executed when the cookie matches.
RewriteRule /inbox$ http://127.0.0.1:22131/ [P,C]
RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /(js/qui.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /(css/qui.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /js/soundmanager2-nodebug-jsmin.js
http://127.0.0.1:22131/js/soundmanager2-nodebug-jsmin.js [P,C]
RewriteRule /sound/soundmanager2.swf
http://127.0.0.1:22131/sound/soundmanager2.swf [P,C]
RewriteRule /images/favicon.png http://127.0.0.1:22131/images/favicon.png [P,C]
RewriteRule /(e/.+) http://127.0.0.1:22131/$1 [P,C]
RewriteRule /compose-mail$ /proxy/ [C]
RewriteRule /compose-mail/(.+)$ /proxy/$1

# Processing continues here.

With these flags it's quite possible to make quite complex conditional
structures. I once inherited a server where every request was checked
against 200+ rules. By some clever "if-then-else" logic implemented
using dummy rules and the S flag I managed to reduce that all to just
a handfull of tests for each request :-)

Krist
(Disclaimer: I'm currently on the train to work with no way to test
this... This is all of the top of my head.)


--
krist.vanbesien@gmail.com
krist@vanbesien.org
Bremgarten b. Bern, Switzerland
--
A: It reverses the normal flow of conversation.
Q: What's wrong with top-posting?
A: Top-posting.
Q: What's the biggest scourge on plain text email discussions?

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 11:34:39 von Perl Whore

I read the docs for the flags and it says for [C] that if a rule is
not matched then it skips the rest of the rules.

What I'm trying to do are set individual rules for the same condition
and not a chain of rules. So only the rule that match should be
applied.

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 12:37:54 von Cristian Pini

It skips the rest of the rules that are chained with that rule if it does n=
ot match, not necessarily the rest of the conditions that are set.

Can you add the code pls as the next part seems a little unclear.


-----Original Message-----
From: Perl Whore [mailto:whoreperl@gmail.com]
Sent: 18 December 2009 10:35
To: users@httpd.apache.org
Subject: Re: [users@httpd] What is wrong with this RewriteCond?

I read the docs for the flags and it says for [C] that if a rule is
not matched then it skips the rest of the rules.

What I'm trying to do are set individual rules for the same condition
and not a chain of rules. So only the rule that match should be
applied.

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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


------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 13:34:50 von Perl Whore

I got it to work correctly with the [L] flag. Now everything is
working as intended.

One last thing I need to accomplish is overriding the Server header
when using the [P] flag. For example in this rule

RewriteRule /inbox/(.+) http://127.0.0.1:22131/$1 [P]

The Server header returned is "TwistedWeb/2.5.0" (python server). Can
I do something to override that and make it return the Apache version
like it does normally?

On Fri, Dec 18, 2009 at 5:07 PM, Cristian Pini wrote:
> It skips the rest of the rules that are chained with that rule if it does=
not match, not necessarily the rest of the conditions that are set.
>
> Can you add the code pls as the next part seems a little unclear.
>
>
> -----Original Message-----
> From: Perl Whore [mailto:whoreperl@gmail.com]
> Sent: 18 December 2009 10:35
> To: users@httpd.apache.org
> Subject: Re: [users@httpd] What is wrong with this RewriteCond?
>
> I read the docs for the flags and it says for [C] that if a rule is
> not matched then it skips the rest of the rules.
>
> What I'm trying to do are set individual rules for the same condition
> and not a chain of rules. So only the rule that match should be
> applied.
>
> ------------------------------------------------------------ ---------
> The official User-To-User support forum of the Apache HTTP Server Project=
..
> See for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> =A0 " =A0 from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>
> ------------------------------------------------------------ ---------
> The official User-To-User support forum of the Apache HTTP Server Project=
..
> See for more info.
> To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> =A0 " =A0 from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
>
>

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 14:48:42 von Krist van Besien

On Fri, Dec 18, 2009 at 11:34 AM, Perl Whore wrote:
> I read the docs for the flags and it says for [C] that if a rule is
> not matched then it skips the rest of the rules.

I told you my example was "advanced"...


> What I'm trying to do are set individual rules for the same condition
> and not a chain of rules. So only the rule that match should be
> applied.

Only rules that match will ever be applied. Basically apache just
tries all the rules in sequence, and if they match the rule gets
applied. That means that several rules can be applied, unless rewrite
rule processing stops (because of a flag)









--
krist.vanbesien@gmail.com
krist@vanbesien.org
Bremgarten b. Bern, Switzerland
--
A: It reverses the normal flow of conversation.
Q: What's wrong with top-posting?
A: Top-posting.
Q: What's the biggest scourge on plain text email discussions?

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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: What is wrong with this RewriteCond?

am 18.12.2009 16:13:32 von Perl Whore

I got it to work after going through the logs.

The next thing I need to do is make it override the Server header of
the internal server. Can that be done with mod_proxy?

------------------------------------------------------------ ---------
The official User-To-User support forum of the Apache HTTP Server Project.
See 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