redirect depending on cookies (more complex example)

Oh, the strange things mod_rewrite does!

redirect depending on cookies (more complex example)

Postby forearrow » Sun Jun 10, 2007 3:58 am

hi, richardk, thank you for your help, I'm sorry to trouble you again, I have one more complex question, I've tried many times, but still can't get it through, would you please give me a hand once more?
here is the more complex question:

the rewrite rule must do those things:
1. when the url include query string,change '?' in to '_'
for example:
http : //localhost/action.do?param=val => http : //localhost/action.do_param=val
(action.do_param=val is a static html file exist in the wwwroot)

2. when has a cookie named 'lang' but has no query string,append '_lang=cookieValue' to the url (cookie 'lang' has three prosible value,that is en,tw,cn)
for example:
when cookie lang=en
http : //localhost/action.do => http : //localhost/action.do_lang=en
(action.do_lang=en is a static html file exist in the wwwroot)
when cookie lang=tw
http : //localhost/action.do => http : //localhost/action.do_lang=tw
(action.do_lang=tw is a static html file exist in the wwwroot)

3. when has a cookie named lang and has query string,change '?' in to '_', at the same time,append '&lang=cookieValue' to the url.
for example:
when cookie lang=en
http : //localhost/action.do?param=val => http : //localhost/action.do_param=val&lang=en
(action.do_param=val&lang=en is a static html file exist in the wwwroot)
when cookie lang=tw
http : //localhost/action.do?param=val => http : //localhost/action.do_param=val&lang=tw
(action.do_param=val&lang=tw is a static html file exist in the wwwroot)
forearrow
 
Posts: 11
Joined: Fri Apr 27, 2007 10:37 pm

Postby richardk » Sun Jun 10, 2007 9:26 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(.+)$ /$1_%1 [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(lang=(en|tw|cn))(;\ .*)?$ [NC]
RewriteRule ^(.+)$ /$1_%2 [QSA,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} .
RewriteCond %{HTTP_COOKIE} ^(.*;\ )?(lang=(en|tw|cn))(;\ .*)?$ [NC]
RewriteRule ^(.+)$ /$1_%{QUERY_STRING}&%2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby forearrow » Mon Jun 11, 2007 9:57 pm

:wink: thanks richardk
forearrow
 
Posts: 11
Joined: Fri Apr 27, 2007 10:37 pm


Return to Idiosyncrasies

Who is online

Users browsing this forum: No registered users and 3 guests

cron