Forum Moderators: phranque
I don't know why this rule is not working. I am trying to forbid the access to [<MyDomain.com...] if the cookie is not equal to MYCOOKIE. Below is my code
#########
RewriteEngine on
RewriteCond %{HTTP_COOKIE}!MYCOOKIE
RewriteRule ^/myhome/content/(.*)$ - [F]
########
But somehow i can still access the page without cookie not being set/correct.
I appreciate if someone can point me in right direction
Thanks
I'd suggest the following tweak to the code, but I don't see anything really wrong:
RewriteEngine on
RewriteCond %{HTTP_COOKIE} !MYCOOKIE
RewriteRule ^/myhome/content/ - [F]
If that doesn't work, try a temporary 'test' redirect, so that you'll be able to see the cookie value that Apache is seeing:
RewriteEngine on
RewriteRule ^/myhome/content/testcookie.html http://www.example.com/?cookie_value=%{HTTP_COOKIE} [R=302,L]
Jim
I'm not sure about Apache version differences in *checking* cookies, though -- I haven't done it myself, which is why my answers were general. Apache was in existence before cookies were invented, so check your specific 1.x version, and the release notes for several versions 'on either side' of it.
Jim
now going back to my rule, I am trying to allow only the JPG files to be accessible even though the Cookie doesn't exist/match but it should restrict all other contents.
So I modified my code in this way
#########
RewriteEngine on
RewriteCond %{HTTP_COOKIE}!MYCOOKIE
RewriteRule ^/myhome/content/.*\.jpg$ - [R=301,L]
RewriteRule ^/myhome/content/(.*)$ - [F]
########
This is working partially, means it is doing exactly what i need when the Cookie doesn't match
but it also forbidding the access to /myhome/content/... when the Cookie exists/matches
Or do you think i should write two separte conditions and rules like this
#########
RewriteEngine on
RewriteCond %{HTTP_COOKIE}!MYCOOKIE
RewriteRule ^/myhome/content/.*\.jpg$ - [R=301,L]
RewriteCond %{HTTP_COOKIE} MYCOOKIE
RewriteRule ^/myhome/content/(.*)$ - [R=301,L]
########
Appreciate your help in this regard.
Thanks Again
1) RewriteConds apply only to the single rule that follows them.
2) Both cases in your code would have created an infinite redirect loop, since they redirected to the same URL as was requested.
A slight rearrangement and tweak of the code might help:
RewriteEngine on
#
# If incorrect or missing cookie, block all except .jpg requests with 403-Forbidden response
RewriteCond %{REQUEST_URI} !\.jpg$
RewriteCond %{HTTP_COOKIE} !MYCOOKIE
RewriteRule ^/myhome/content/ - [F]