Forum Moderators: phranque

Message Too Old, No Replies

can mod rewrite temporarily remember certain values?

         

crimsontwo

3:34 am on Mar 25, 2010 (gmt 0)

10+ Year Member



hi,

here's my code:

RewriteCond %{QUERY_STRING} a=([^/]+)&b=([^/]+)&c=([^/]+)

RewriteRule ^(.*) http :// %{HTTP_HOST }/test/%1/%2/?

However, I then have another rule:

RewriteRule ^test/([^/]+)/([^/]+)/$ script.php?c=...

I need C to be %3 from the first RewriteCond. Is it possile? What would be the workaround then?

Thanks.

crimsontwo

4:27 am on Mar 25, 2010 (gmt 0)

10+ Year Member



OK, I did it through cookies:

RewriteRule ^(.*) http :// %{HTTP_HOST }/test/%1/%2/? [CO=mycookie:%3:.%{HTTP_HOST}]

RewriteCond %{HTTP_COOKIE} mycookie=([^;]+)
RewriteRule ^test/([^/]+)/([^/]+)/$ script.php?c=%1 [L]

---

it works. is there a better/more elegant way? please point to my mistakes, if any.

thanks.

jdMorgan

2:12 pm on Mar 26, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't want to use the cookie method, then you can't delete the "c=" parameter before redirecting:

RewriteCond %{QUERY_STRING} a=([^&]+)&b=([^&]+)&c=([^&]+)
RewriteRule ^(.*)$ http://%{HTTP_HOST }/test/%1/%2/?c=%3 [R=301,L]
#
RewriteCond %{QUERY_STRING} ^([^&]+&)*c=([^&]+)
RewriteRule ^test/([^/]+)/([^/]+)/$ script.php?c=%2 [L]

Even if you do decide to keep the cookie method, note the many other small changes to the patterns and syntax above. Many of them would still apply to your "cookie code."

Jim

crimsontwo

2:31 pm on Mar 26, 2010 (gmt 0)

10+ Year Member



Great, noted. Thank you.

crimsontwo

3:51 pm on Mar 26, 2010 (gmt 0)

10+ Year Member



OK, I now seem to have 2 problems:

1. [R=301,L] throws an error. if I remove R=301, it works.
2. my new URL is now http :// %{HTTP_HOST }/test/something1/something2/?c=something3

the reason why I was doing it through cookies is to avoid showing the "c=something3" in the URL.

any idea how to fix this or your method will always show that unwanted C variable?

jdMorgan

1:01 am on Mar 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1. What error? What is reported in your server error log file?
2. This method must "show" the 'c=' variable, so that the client can 'remember' it.

HTTP is a stateless protocol, so the server does not 'remember' any information about previous HTTP requests when handling a current request. Therefore the 'c=' information has to be present in the client request -- either in the URL-path, the query string, or in a cookie header sent along with the request.

Jim