Forum Moderators: phranque

Message Too Old, No Replies

Limit access to files certain times of day, htaccess

How do I limit access using htaccess

         

pepsiman

10:54 pm on Nov 2, 2006 (gmt 0)

10+ Year Member



Hello

Is it possible to limit access using .htaccess for a certain time period of a day?

Say I don't want visitors between 2am and 6am. Is that possible to do with htaccess and mod_rewrite? And if so, would anyone have any pointers to where I can find more info on it. I've tried my best to search for it but can't find anything related.

Thanks in advance.

jdMorgan

11:34 pm on Nov 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To redirect your home page to an alternate "we're closed" page for human visitors between 2:00 AM and 5:59.59 AM. you could do something like this:

RewriteCond %{TIME_HOUR} ^0[2-5]$
RewriteCond %{HTTP_USER_AGENT} !(Googlebot¦Slurp¦msnbot¦Teoma) [NC]
RewriteRule ^$¦^index\.html$ http://www.example.com/we-are-closed.html [R=302,L]

The second RewriteCond prevents the major search engine spiders from seeing the "We're closed" page in order to avoid totally nuking your search engine ranking.

I would recommend you send the Vary: User-Agent header in response to home page requests, since the content will now change based on the requesting user-agent, and this can cause problems with network caches. You will also need to mark the "We're closed" page as expiring at 6:00 AM. Use Apache mod_headers and mod_expires to mark the files appropriately.

You will also need to put the <meta name="robots" content="nocache"> tag on every page that your want to limit access to. Otherwise, the original page will alwasy be shown in the search engines' cached-page views, which will partially defeat your purpose.

I can't say I really recommend this overall approach, though, because the Web is never closed. If you have some specific problem, like phone calls in the middle of the night, you might consider fixing just that problem, by not showing the phone number at night, for example.

Replace the broken pipe "¦" character above with a solid pipe character from your keyboard before use; Posting on this board modifies the pipe character.

For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim

[edited by: jdMorgan at 11:35 pm (utc) on Nov. 2, 2006]

pepsiman

7:20 am on Nov 4, 2006 (gmt 0)

10+ Year Member



Thanks a million, this is exactly what I was looking for. I must re-read the docs on mod_rewrite as this one passed me by...! :-D

The reason for having 'opening hours' is because the moderator of a specific forum I host wants her forum closed when she's not around... I have strongly recommended against it but it is her forum and I'll do anything for her :-D