My 2nd % variable is not being found in th redirected UR

Oh, the strange things mod_rewrite does!

My 2nd % variable is not being found in th redirected UR

Postby movieguy » Sun Jul 05, 2009 10:35 am

Hi to All,

I have a problem that I can't solve, though the code looks correct to me.
I'm using two RewriteConditions using two COOKIES to control a Rewrite Rule that, if the cookies are not on the computer, will redirect to the Indexx2.php page. That works fine.
I have a further Rewrite rule controlled by four additional RewriteConditions:
If three of the four cookies exist, then the visitor is redirected to the Login page. This is the broken part; it is redirecting to the URL but it is leaving part of the URL out.

I have declared two of the COOKIES as variables with (), and I've inserted them into the redirect absolute URL.
One of them, the Login_%1, works just fine but the %3 simply does nothing; it won't deploy the value.

I've rewritten this so many times :o that I hardly know what is what anymore but this current configuration seems to get me 2/3 of the way there.

I'm on Apache 1.3xx and I cannot for the life of me, (one week running now) figure out why I can't pick up the other variable; it just leaves an empty // in the URL where the word should show up.

All help is greatly appreciated.


Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_COOKIE} !prime_lang.*$ [NC]
RewriteCond %{HTTP_COOKIE} !targ_lang.*$ [NC]
RewriteRule .* http://www\.mysite\.com/Indexx2\.php [NC,R]
RewriteCond %{HTTP_COOKIE} !targ_nationality=[a-zA-Z] [NC]
RewriteCond %{HTTP_COOKIE} prime_lang=([a-zA-Z]+) [NC]
RewriteCond %{HTTP_COOKIE} targ_lang=[a-zA-Z]+ [NC]
RewriteCond %{HTTP_COOKIE} level1=([a-zA-Z]+) [NC]
RewriteRule .* http://www\.mysite\.com/Login/%2/Login_%1\.php [NC,R]

The Login_%1 outputs the level1 COOKIE , i.e. either Student or Teacher. and it works fine.
The %2 should output the name of the language, i.e. /English/ and it dun woik at all.
The final URL should read as:
www.mysite.com/Login/English/Login_Student.php or
www.mysite.com/Login/German/Login_Teacher.php or whatever the variable happens to be. But all I'm getting is
www.mysite.com/Login//Login_Student.php

Lastly, thank you for reading this and responding.
movieguy
 
Posts: 4
Joined: Sun Jul 05, 2009 10:02 am

Postby richardk » Mon Jul 06, 2009 8:30 am

You can only access backreferences/variables from the last RewriteCond. You need to pass anything you want to keep to the next RewriteCond.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_COOKIE} !^(.*;\ )?prime_lang=[a-z]+(;\ .*)?$ [NC]
RewriteCond %{HTTP_COOKIE} !^(.*;\ )?targ_lang=[a-z]+(;\ .*)?$ [NC]
RewriteRule .* http://www.example.com/Indexx2.php [R,L]

RewriteCond %{HTTP_COOKIE} !^(.*;\ )?targ_nationality=[a-z]+(;\ .*)?$ [NC]
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?targ_lang=[a-z]+(;\ .*)?$ [NC]
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?prime_lang=([a-z]+)(;\ .*)?$ [NC]
# Pass it on here.
RewriteCond %2;%{HTTP_COOKIE} ^([^;]+);(.*;\ )?level1=([a-z]+)(;\ .*)?$ [NC]
RewriteRule .* http://www.example.com/Login/%1/Login_%3.php [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby movieguy » Mon Jul 06, 2009 5:52 pm

Thanks Richard!

As I look at this tonight, I'll work through your suggestion (it's working great as far as I can see) and through your changes. I may have an additional question to ask on this later if you don't mind.

Thanks again.

Brian
movieguy
 
Posts: 4
Joined: Sun Jul 05, 2009 10:02 am

Postby movieguy » Mon Jul 06, 2009 7:27 pm

Okay, that worked well and I sort of understood most of it. But, rather than me screwing around trying to add to it, I thought I'd just ask you this:

The Login page, to which the code redirects the visitor in this instance, needs to have a value appended to the end of the redirect URL. In normal, earth code, it looks like this:

http://www.example.com/Login/English/Lo ... lang=Farsi with Farsi simply being whatever URL param value of the variable %targ_lang. Would you please show me how to append that to the end of the http://www.example.com/Login/English/Login_Student.php, including that QSA thing that appends query strings? And thank you again; That is really a 'Through the Looking Glass' piece of voodoo.
movieguy
 
Posts: 4
Joined: Sun Jul 05, 2009 10:02 am

Postby richardk » Tue Jul 07, 2009 8:37 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{HTTP_COOKIE} !^(.*;\ )?prime_lang=[a-z]+(;\ .*)?$ [NC]
RewriteCond %{HTTP_COOKIE} !^(.*;\ )?targ_lang=[a-z]+(;\ .*)?$ [NC]
RewriteRule .* http://www.example.com/Indexx2.php [R,L]

RewriteCond %{HTTP_COOKIE} !^(.*;\ )?targ_nationality=[a-z]+(;\ .*)?$ [NC]
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?targ_lang=([a-z]+)(;\ .*)?$ [NC]
Pass on targ_lang.
RewriteCond %2;%{HTTP_COOKIE} ^([^;]+);(.*;\ )?prime_lang=([a-z]+)(;\ .*)?$ [NC]
# Pass it on targ_lang and prime_lang.
RewriteCond %1;%3;%{HTTP_COOKIE} ^([^;]+);([^;]+);(.*;\ )?level1=([a-z]+)(;\ .*)?$ [NC]
RewriteRule .* http://www.example.com/Login/%2/Login_%4.php?targ_lang=%1 [R,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby movieguy » Tue Jul 07, 2009 2:14 pm

Well that's got it!!!
I couldn't reach you by email but I want very much to say thank you for fixing this for me. I did not know the drop down rule even existed and it would have been another week before I got the combination of things correct. Is there something I can do for you? Have you written a mod_rewrite book I could buy? Do you have an Amazon.com wish list? I'd like to say thanks some how because, at this point in the project, that was really the last dig to bedrock and now we start building afresh.

Please let me know if I can do anything to say thanks.
movieguy
 
Posts: 4
Joined: Sun Jul 05, 2009 10:02 am


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 6 guests

cron