Forum Moderators: phranque

Message Too Old, No Replies

Checking value of cookie to restrict access

         

pportiz

10:20 am on Sep 4, 2007 (gmt 0)

10+ Year Member



Hi all,

I was trying to limit access to certain html pages of my site in function of the value of a cookie I set. I was trying to do it with mod rewrite, the idea is the next:

RewriteCond %{HTTP_COOKIE} name_of_cookie=([^;]+)
RewriteCond %{THE_REQUEST}!%1
RewriteRule ^/conflict_directory/.*\.html?$ /errores/notsofast.html

The problem is the second parameter of RewriteCond is a regular expression and I cannot use the value of %1.

Can anybody help?

Thanks in advance.

jdMorgan

1:14 pm on Sep 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Back-references such as %1 cannot be used in mod_rewrite patterns, and mod_rewrite has no way of comparing two variables internally. On some OSes, you can use a POSIX 1003.2 atomic back-reference hack to do it, but that solution is non-portable to other OSes.

Please tell us more about the value of the cookie; What is its meaning, and how can its valid values vary? -- This may help us to suggest an alternate approach.

Jim

pportiz

7:59 am on Sep 5, 2007 (gmt 0)

10+ Year Member



Hi,

in order to allow the visitor to get the html, the value of the cookie must be the same that appears on the URL:

/conflict_directory/VALUE_OF_THE_COOKIE/path_to_file/file.html

I've implemented a partial solution:

RewriteCond %{HTTP_COOKIE} curso=([^;]+) [NC]
RewriteRule ^/conflict_directory/([^/])+/(.*.html?) /mir8/lecciones/%1/$2

As I cannot compare two variables I redirect to the correct file if the visitor has the appropiate cookie, and in other case he is redirected to the directory where he has access, normally getting a Not Found Error unless the path is the same, but he doesn't get the file he isn't allowed to get.

This gives me the functionality, but I would like to be able to send him to an special error page.

Any idea?

Thanks for your interest.

jdMorgan

2:09 pm on Sep 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can test to see if the "valid page" exists, and if not, rewrite to the "special page":

# Get cookie value if it exists
RewriteCond %{HTTP_COOKIE} curso=([^;]+)
# if valid page exists as a file
RewriteCond %{DOCUMENT_ROOT}/mir/lecciones/%1/$2 -f
# internally rewrite to valid page
RewriteRule ^/conflict_directory/([^/])+/(.+\.html?)$ /mir8/lecciones/%1/$2 [L]
# else rewrite to special page
RewriteRule ^/conflict_directory/([^/])+/(.+\.html?)$ /special_page.html [L]

Jim

pportiz

2:41 pm on Sep 5, 2007 (gmt 0)

10+ Year Member



Thanks!

That solved my problem.