I need a rewritecond for htaccess for redirect to a site based on time and day.
Ex:
if its monday,tuesday,wednesday,thursday or friday between 18:00 and 7:00 RedirectMatch 301 ^/.*$ https://example.com
if its monday,tuesday,wednesday,thursday or friday between 7:01 and 17:59 RedirectMatch 301 ^/.*$ https://example.com/we-are-closed-come-back-later.html
if its saturday or sunday RedirectMatch 301 ^/.*$ https://example.com/weekend.html
I tried something like this:
RewriteCond %{TIME_HOUR} ^22$
RewriteCond %{TIME_MIN} !^00$
RewriteCond %{TIME_WDAY} =0
RewriteRule !^night https://example.com [L,R]
but need it more specific as written.
any idea?
CodePudding user response:
Try the following instead:
RewriteEngine On
# 1. Mon-Fri between 07:01 and 17:59 - CLOSED
RewriteCond %{TIME_WDAY} [12345]
RewriteCond %{TIME_HOUR}%{TIME_MIN} >0700
RewriteCond %{TIME_HOUR}%{TIME_MIN} <1800
RewriteRule !^we-are-closed-come-back-later\.html$ /we-are-closed-come-back-later.html [R,L]
# 2. Sat-Sun - WEEKEND
RewriteCond %{TIME_WDAY} [06]
RewriteRule !^weekend\.html$ /weekend.html [R,L]
# 3. Otherwise redirect to homepage (OPEN)
RewriteRule !^(weekend\.html|we-are-closed-come-back-later\.html)?$ / [R,L]
TIME_WDAY
is the day of the week from 0
Sunday to 6
Saturday.
>0700
is a lexicographical string comparison (not a numeric comparison). So, this is true if HHMM is 0701
or greater.
You don't necessarily need any conditions for the last rule, since if the preceding rules do not match then it must be open. However, this does mean that weekend.html
and we-are-closed-come-back-later.html
could be accessed directly regardless of whether it is "weekend" or "closed". This could be avoided by including additional conditions.
For example:
# 3. Mon-Fri between 18:00 and 07:00 (OPEN)
RewriteCond %{TIME_WDAY} [12345]
RewriteCond %{TIME_HOUR}%{TIME_MIN} >=1800
RewriteCond %{TIME_HOUR}%{TIME_MIN} <=0700
RewriteRule !^$ / [R,L]
Alternatively, rewrite the URL internally instead of redirecting?