Jump to content

mod_rewrite based on cookie value - regexp in mod_rewrite ?


Mark Baker

Recommended Posts

I'm trying to create a rewrite rule based on the value of a cookie rather than simply whether one is set or not.

 

The following code tests for the existence of the cookie IRISVersion, which (if it exists) will contain either an empty string, or the name of a subdomain (e.g. 'subdomain1.','subdomain2.').

 

%{HTTP_HOST} is 'mydomain.com'

 

The intent is to redirect to subdomain1.mydomain.com, subdomain2.mydomain.com or mydomain.com as appropriate.

 

RewriteEngine on
RewriteCond %{HTTP_COOKIE} CodeVersion
RewriteRule /* http://%{HTTP_COOKIE}%{HTTP_HOST}/subdomainTest.html [R,L]

 

However, the rewrite rule is picking up the value of all cookies set, and substituting in the entire set of values for the redirect.

So I'm getting a redirect to

http://codeversion=subdomain1./;%20login=truemydomain.com/subdomainTest.html

 

I need some way to extract the actual value of the CodeVersion cookie for the rewrite rule, ignoring any other cookies that might be set. I suspect I'll probably need some for of regexp

 

 

Alternatively, might a rewritemap provide an alternative solution

 

Using Apache 2.2.6

 

 

Any help gratefully appreciated

Link to comment
Share on other sites

I've modified my cookie so that the empty string is no longer valid, so the CodeVersion cookie should now always contain a valid subdomain name, if it exists

 

Alternative version of the mod_rewrite, trying to extract the cookie value in the rewrite condition

 

RewriteCond %{HTTP_COOKIE} CodeVersion=([a-z0-9]+)
RewriteRule /* http://$1%{HTTP_HOST}/subdomainTest.html [R,L]

 

Still feel like I'm getting nowhere, other than into an infinite loop of redirection

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.