You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Alan Little <da...@holotech.net> on 2006/03/09 14:31:50 UTC

[users@httpd] Cookie-Based Rewrite

Sorry if this has been asked before; I tried to find if there were
searchable archives for this list, but couldn't find anything.

I want to do a rewrite based on the value of a cookie. I have a rule in
doc root for the domain, which directs it to the demo directory based on 
the third-level domain:

  RewriteCond %{HTTP_HOST} ^demo.example.com$
  RewriteCond %{REQUEST_URI} !^/demo/
  RewriteRule (.*) /demo/$1

This works fine. From there, I want to re-direct to a sub-directory with 
the same name as the value of the cookie phn_demo, if the cookie is
set. I tried this:

  RewriteCond %{HTTP_COOKIE} phn_demo=(.*)
  RewriteRule (.*) /demo/%1/$1

but get a 500 error, which strangely doesn't show up in the logs. If the 
cookie is not set, I don't get an error.

-- 
Alan Little
Holotech Enterprises


---------------------------------------------------------------------
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] Cookie-Based Rewrite

Posted by Alan Little <da...@holotech.net>.
OK, so I got this figured out, and thought I'd post it here for
posterity.

Rather than putting the cookie rule in a separate .htaccess file in
the demo directory, I put them both in the root:

RewriteCond %{HTTP_HOST} ^demo.example.com$
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{HTTP_COOKIE} phn_demo=([^;]+)
RewriteRule (.*) /demo/%1/$1 [L]

RewriteCond %{HTTP_HOST} ^demo.example.com$
RewriteCond %{REQUEST_URI} !^/demo/
RewriteRule (.*) /demo/$1

It works wonderfully. I just realized I should probably remove the $1
from the second rule, though, as I don't want them going anywhere or
getting 404s if they don't have a cookie. Damned cool voodo.

-- 
Alan Little
Holotech Enterprises

On Thursday, March 9, 2006, 8:31:50 AM, you wrote:


> Sorry if this has been asked before; I tried to find if there were
> searchable archives for this list, but couldn't find anything.

> I want to do a rewrite based on the value of a cookie. I have a rule in
> doc root for the domain, which directs it to the demo directory based on
> the third-level domain:

>   RewriteCond %{HTTP_HOST} ^demo.example.com$
>   RewriteCond %{REQUEST_URI} !^/demo/
>   RewriteRule (.*) /demo/$1

> This works fine. From there, I want to re-direct to a sub-directory with
> the same name as the value of the cookie phn_demo, if the cookie is
> set. I tried this:

>   RewriteCond %{HTTP_COOKIE} phn_demo=(.*)
>   RewriteRule (.*) /demo/%1/$1

> but get a 500 error, which strangely doesn't show up in the logs. If the
> cookie is not set, I don't get an error.



---------------------------------------------------------------------
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[2]: [users@httpd] Cookie-Based Rewrite

Posted by Alan Little <da...@holotech.net>.
Thank you. Unfortunately, this is on a shared hosting server, and I
don't have access to the config files, nor the re-write log (if there
is one).

I figured out after I posted my question that I needed to exclude the
/demo/ directory, since that's where this is. I tried the regex you
suggested -- it makes more sense. But I still get the error. If I add
the RewriteCond for REQUEST_URI, I no longer get the error, but no
rewrite, either.

I must say, most of what I do with mod_rewrite is MSMD. I don't really
understand how it works, except in the most general terms. It would be
great if there was something that explained what exactly mod_rewrite
does, step by step, but all the tutorials I've been able to find are
really just recipes for doing various things, without really
explaining what's going on.

From what I understand, the rule as you wrote it goes:

> RewriteCond %{REQUEST_URI} !^/demo/

"If REQUEST_URI does not begin with /demo/"

> RewriteCond %{HTTP_COOKIE} phn_demo=([^;]+)

"...and HTTP_COOKIE contains 'phn_demo=' followed by a value"

> RewriteRule ^(.*) /%1/$1 [L]

"...rewrite the request to point to directory %1 (the value from
RewriteCond) below this directory, followed by whatever's left of
the URI."

Isn't that correct? It seems pretty straightforward to me, but for
some reason doesn't work.

-- 
Alan Little
Holotech Enterprises

On Thursday, March 9, 2006, 11:38:34 AM, you wrote:


> Alan Little wrote:
>> I have a rule in
>> doc root for the domain

> Why don't you use the Rules in per-server context (httpd.conf), many 
> things are a lot easier there, because there is no internal redirect and
> the regEx is only compiled once at startup and not per each request.

> What can you find in your rewrite.log? Or are the rules not processed?

>>   RewriteCond %{HTTP_COOKIE} phn_demo=(.*)
>>   RewriteRule (.*) /demo/%1/$1

> If you're using the rule in per-dir context, you must exclude the folder
> /demo/ as you did for the other rule. May be you must separate the group
> from other cookies, too?

> RewriteEngine on
> RewriteCond %{REQUEST_URI} !^/demo/
> RewriteCond %{HTTP_COOKIE} phn_demo=([^;]+)
> RewriteRule ^(.*) /demo/%1/$1 [L]



---------------------------------------------------------------------
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] Cookie-Based Rewrite

Posted by Robert Ionescu <ro...@googlemail.com>.
Alan Little wrote:
> I have a rule in
> doc root for the domain

Why don't you use the Rules in per-server context (httpd.conf), many 
things are a lot easier there, because there is no internal redirect and 
the regEx is only compiled once at startup and not per each request.

What can you find in your rewrite.log? Or are the rules not processed?

>   RewriteCond %{HTTP_COOKIE} phn_demo=(.*)
>   RewriteRule (.*) /demo/%1/$1

If you're using the rule in per-dir context, you must exclude the folder 
/demo/ as you did for the other rule. May be you must separate the group 
from other cookies, too?

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/demo/
RewriteCond %{HTTP_COOKIE} phn_demo=([^;]+)
RewriteRule ^(.*) /demo/%1/$1 [L]

-- 
Robert

---------------------------------------------------------------------
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