基于时间和天的htaccess重定向到网站
我需要一个基于时间和日的重定向到网站的HTACCESS重新定位。
例如:
如果它的星期一,星期二,星期三,周四或星期五18:00至7:00 redirectMatch 301 ^/。*$ https://example.com
如果它 或星期五7:01至17:59 RedirectMatch 301 ^/。
星期四 星期日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?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
改用以下内容:
time_wday
是一周中的一天,0
星期日到6
星期六。> 0700
是词典字符串比较(不是数字比较)。因此,如果HHMM为0701
或更高版本,这是正确的。在每个规则中,我们需要检查我们的不是在文件/URL上的自然会触发重定向循环。
您不一定需要在最后一个规则(“打开”时检查天/时间)的任何条件(检查天数),因为如果前面的规则不匹配(即关闭或周末),则必须打开它。
例如:
但是,这意味着
weekend.html
和We-are-close-come-come-back-later.html
可以直接访问(通过键入URL)当商店实际上是“开放的”时。静态资源 /资产(图像,JS,CSS,...)
但是,以上未考虑您可能要链接到本地的任何静态资源。如果没有其他规则,以上还将将这些静态资源重定向到所述的URL/文件,即。
/weekend.html
等。理想情况下,任何静态资产都会在
/Assets
子目录中分组在一起,您可以简单地对此子目录进行例外您现有的规则。例如:或者,您对映射到物理文件的任何请求都例外。但是,我们需要排除上面使用的两个文件(即
We-close-closed-come-back-later.html
和weekend.html
),否则,这些无论如何,都可以直接访问请求(如上所述)。例如:重写(内部)而不是重定向(外部)
,您可以内部重写URL而不是重定向。然后,您只有一个公共URL(即文档root)和基础文件(即
Weekend.html
或We-are-close-closed-come-come-back-later.html )可以从用户隐藏。
Try the following instead:
TIME_WDAY
is the day of the week from0
Sunday to6
Saturday.>0700
is a lexicographical string comparison (not a numeric comparison). So, this is true if HHMM is0701
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:
However, this would mean that
weekend.html
andwe-are-closed-come-back-later.html
could be accessed directly (by typing the URL) when the shop is actually "OPEN".Static Resources / Assets (images, JS, CSS, ...)
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: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
andweekend.html
), otherwise, these would be directly accessible regardless of when the request is made (as mentioned above). For example:Rewrite (internally) instead of Redirect (externally)
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
orwe-are-closed-come-back-later.html
) can be hidden from the user.