mod_rewrite redirect on if no cookie

Hi All,

I need a little help with my rewrite statement, it’s close but not 100%. I want to redirect to a sub folder /launchpage/ if there is no particular cookie found. The redirect needs to happen for any pages within the root directory. At the moment it is redirecting everything cookie or no cookie.


RewriteEngine On
RewriteBase /

# redirect if cookie doesn't exist
RewriteCond %{HTTP_COOKIE} !^.*cookiename.*$ [NC]

# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI} !^/launchpage/

# perform redirect
RewriteRule ^(.*)$ /launchpage/ [R,NC,L]

Thankyou

lee,

May I assume that .cookiename. is the name of the cookie? If so, it’s in the location where REGEX is required to match the %{HTTP_COOKIE} variable. In other words, each .* is the same as the :kaioken: EVERYTHING :kaioken: atom (except that it’s not being captured).

Yes, you DO want to capture the name of the cookie but ALL the cookie’s text is returned by {HTTP_COOKIE}. Echo it out and see what it provides and only capture the cookie’s name.

Regards,

DK

Thanks for the reply DK. Yes cookiename is the name of the cookie.

Do you mean that I should change that line to as follows? I tried this also but it didn’t work.

RewriteCond %{HTTP_COOKIE} !^cookiename$ [NC]

lee,

I haven’t looked at cookie string contents in years but I’d guess that the name is the first test in the string so the start anchor can stay but I’d then “lose” the end anchor as the contents of the string probably will vary for each visitor with that cookie. Please remember that I suggested that you print a {HTTP_COOKIE} string so you can see what’s there to know if anything precedes the cookiename and just what comes after (you should look for a delimiter as that would help terminate the value of the name).

As an aside, the No Case flag will capture CoOkIeNaMe, too, but, because you set the cookie, you don’t need that!

Regards,

DK

Thanks again.

I have now tried removing the start and end anchors but it still doesn’t work in any combo of these. I am unsure what you mean by printing the {HTTP_COOKIE} string, is this done within the htaccess file somehow?

Lee,

I’d try echo $_SERVER[‘HTTP_COOKIE’];

Regards,

DK

Ahh now I get you, with PHP.

I did this and I got the following:

cookiename=YES

lee,

That is all you’re setting in your cookie? No domain, no expiration date (so it won’t be persistant), no yadda-yadda? Then, if you want to test the entire string, reinstitute the start and end anchors around cookiename=YES (^cookiename=YES$ but don’t forget the NOT’s !).

Regards,

DK

Thanks again, that didn’t seem to work.

Here is my set line:

setcookie('nonstop', "YES", time()+60*60*24*365);

And here is the latest REWRITE CODE


RewriteEngine On
RewriteBase /

# redirect if cookie doesn't exist
RewriteCond %{HTTP_COOKIE} !^nonstop=YES$ [NC]

# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI} !^/launchpage/

# perform redirect
RewriteRule ^(.*)$ /launchpage/ [R,NC,L]

It’s still redirecting everything…

Thanks

lee,

The longevity didn’t show in your earlier post. I wonder why. Please take a look at your cookies and see what it show as there.


RewriteEngine On
[COLOR="Gray"]RewriteBase /[/COLOR]
# I always "pan" RewriteBase if there is no Redirect being corrected

# redirect if cookie doesn't exist
RewriteCond %{HTTP_COOKIE} !^nonstop=YES$ [COLOR="Gray"][NC][/COLOR]

# Only apply to URLs that aren't already under folder.
RewriteCond %{REQUEST_URI} !^[COLOR="Red"]/[/COLOR]launchpage/
[COLOR="Red"]# ONLY for Apache 1.x![/COLOR]

# perform redirect
RewriteRule .? [COLOR="Gray"]/[/COLOR]launchpage/ [R=301,[COLOR="Red"]NC,[/COLOR]L]
# If you're not using an atom, don't create it
# Leading / in the redirect will look at your server's root first AND
# it's not needed in the DocumentRoot's .htaccess
# It's always better to specify the DirectoryIndex file rather than just the directory
# R is default to 302 (temporary) so, if you meant for it to be seen,
# R=301 otherwise it's just not needed
# NC is BAD in the RewriteRule as you never know what you'll get
# in terms of CaMeLcOdE and, because you were trying to capture
# :kaioken: EVERYTHING :kaioken: , there is no need for it anyway

On the assumption that cookiename is actually “nonstop” then this should work (depending upon what cookie information is being sent to the server). If it doesn’t work, eliminate both anchors and give it another try.

Regards,

DK