View Full Version : cookies .htaccess & mod_rewrite
BornOnline
09-04-2006, 02:43 AM
Can anyone give me an example of using .htaccess to look for a specific cookie and if the cookie is not present send that visitor to a specific URL?
Thanks for any info. I'm searching, but have not found what I'm looking for and thought I would ask :D
DavidP
09-04-2006, 04:22 AM
Why not just do that using php?
asterisk
09-04-2006, 07:31 AM
Yeah I agree with DavidP - while you can definitely do that with htaccess and mod_rewrite, I find PHP to be a much cleaner implementation. The former tends to require some basic knowledge of regular expressions which can be a bit messy at times if you're not quite comfortable with it.
BornOnline
09-04-2006, 01:55 PM
Good question.. I guess it's because I don't know how :D
Why not just do that using php?
Here's something basic in PHP:
<?php
// Look for cookie with a name of hello and see that it DOESNT exist first.
if(!$_COOKIE['hello']) {
// The cookie doesn't exist, send visitor to the following URL
Header("Location: http://www.domain.com/url.php");
Exit;
} else {
// The cookie exists
// Put code here or escape PHP and put HTML here.
}
?>
This can obviously be expanded to do whatever you need.
David
dmspilot
09-05-2006, 12:44 AM
I disagree, using mod_rewrite would be cleaner than a PHP script.
RewriteCond %{HTTP_COOKIE} !(cookiename)
RewriteRule .* newpage.html [R]
If you need further help:
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
http://www.ilovejackdaniels.com/apache/mod_rewrite-cheat-sheet/
Why would it be cleaner? Sure it would be intercepted at the apache stage instead of the PHP stage, but with regards to cleanness, they're equal :)
David
asterisk
09-06-2006, 10:37 AM
I apologise. I was thinking 'human-readable' when I wrote 'clean implementation'.
Oh don't forget...
RewriteEngine on
...at the beginning. Something like this should do the trick:
# Tells Apache to follow symbolic links. (Switch this to -FollowSymlinks if this account in
# which htaccess resides in belongs to another user instead for security purposes)
Options FollowSymlinks
RewriteEngine on
# Performs the following if cookie is found with non-case-sensitive name, cookiename.
# Rewrites any existing URL to the following new address and redirect, plus ensures
# no further rewrite rules are executed after this.
RewriteCond %{HTTP_COOKIE} !(cookiename) [NC]
RewriteRule ^.*$ http://www.anotherURL.com/cookienotpresent.htm [R, L]
I just like my code to preferably be human-readable even without comments... like the way sdjl did it with PHP. Easy to read and absolutely beautiful with little room for error. Mod_rewrite can be pretty nasty if you miss out a flag or two, or some odd regexp symbol. Gah.
Asterisk, my reply was referencing dmspilot, i'd failed to see you wrote a pretty similar response!
I too like to write code with comments, unless it's really needed.
Just a quick note, your code will be applicable to the entire website, no matter which page is called, correct?
If so, is there a version which only applies to a certain page, just incase the original questioner wants that :)
David
BornOnline
09-06-2006, 06:59 PM
Thanks for all the info guys!
I really appreciate it.
Hmm.. this is what I'm currently using. It's working, but if someone hits like /blah/ and not /blah using the last backslash the rule does not work. I'll go do some more reading on this.
RewriteCond %{HTTP_COOKIE} !TehCookie [NC]
RewriteRule /blah/* /blah/blah.php?config=&blah=pass&pass=blah_options
asterisk
09-06-2006, 07:34 PM
sdjl, sounds like you were absolutely right on the mark about the OP wanting it to be applied to a certain location. :)
Yeah the original applies to the entire website.
For a specific location, you'd want to do:
# Tells Apache to follow symbolic links. (Switch this to -FollowSymlinks if this account in
# which htaccess resides in belongs to another user instead for security purposes)
Options FollowSymlinks
RewriteEngine on
# Performs the following if cookie is found with non-case-sensitive name, cookiename.
# Redirects all user-requested URLs at /blah or /blah/ to the following
# PHP file and redirect, plus ensures no further rewrite rules are
# executed after this.
RewriteCond %{HTTP_COOKIE} !TehCookie [NC]
RewriteRule ^/blah(/)?$ /blah/blah.php?config=&blah=pass&pass=blah_options&dpp=0z [R, L]
vBulletin® v3.8.4, Copyright ©2000-2010, Jelsoft Enterprises Ltd.