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?
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. 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]
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.
In each rule we need to check that we are not already on the file/URL to be redirected to (eg. !^weekend\.html$
), otherwise, it would naturally trigger a redirect loop.
You don't necessarily need any conditions (to check days/times) on the last rule (when it is "OPEN"), since if the preceding rules do not match (ie. CLOSED or WEEKEND) then it must be open.
For example:
# 3. Otherwise redirect to homepage (OPEN)
RewriteRule !^(weekend\.html|we-are-closed-come-back-later\.html)?$ / [R,L]
However, this would mean that weekend.html
and we-are-closed-come-back-later.html
could be accessed directly (by typing the URL) when the shop is actually "OPEN".
However, the above does not take into account any static resources that you might be linking to locally. Without additional rules, the above would also redirect these static resources to the stated URLs/files, ie. /weekend.html
etc.
Ideally, any static assets would be grouped together in an /assets
subdirectory then you can simply make an exception for this subdirectory before your existing rules. For example:
# 0. Ignore static resources
RewriteRule ^assets/ - [L]
Or, you make an exception for any request that maps to a physical file. However, we need to exclude the 2 files used above (ie. we-are-closed-come-back-later.html
and weekend.html
), otherwise, these would be directly accessible regardless of when the request is made (as mentioned above). For example:
# 0. Ignore static resources
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(weekend\.html|we-are-closed-come-back-later\.html)?$ - [L]
Alternatively, you could rewrite the URL internally instead of redirecting. You then only have one public URL (ie. the document root) and the underlying file (ie. weekend.html
or we-are-closed-come-back-later.html
) can be hidden from the user.