基于时间和天的htaccess重定向到网站

发布于 2025-02-11 05:20:17 字数 546 浏览 0 评论 0原文

我需要一个基于时间和日的重定向到网站的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

很快妥协 2025-02-18 05:20:17

改用以下内容:

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是一周中的一天,0星期日到6星期六。

&gt; 0700是词典字符串比较(不是数字比较)。因此,如果HHMM为0701或更高版本,这是正确的。

在每个规则中,我们需要检查我们的不是在文件/URL上的自然会触发重定向循环。

您不一定需要在最后一个规则(“打开”时检查天/时间)的任何条件(检查天数),因为如果前面的规则不匹配(即关闭或周末),则必须打开它。

例如:

# 3. Otherwise redirect to homepage (OPEN)
RewriteRule !^(weekend\.html|we-are-closed-come-back-later\.html)?$ / [R,L]

但是,这意味着weekend.htmlWe-are-close-come-come-back-later.html可以直接访问(通过键入URL)当商店实际上是“开放的”时。

静态资源 /资产(图像,JS,CSS,...)

但是,以上未考虑您可能要链接到本地​​的任何静态资源。如果没有其他规则,以上还将将这些静态资源重定向到所述的URL/文件,即。 /weekend.html等。

理想情况下,任何静态资产都会在/Assets子目录中分组在一起,您可以简单地对此子目录进行例外您现有的规则。例如:

# 0. Ignore static resources
RewriteRule ^assets/ - [L]

或者,您对映射到物理文件的任何请求都例外。但是,我们需要排除上面使用的两个文件(即We-close-closed-come-back-later.htmlweekend.html),否则,这些无论如何,都可以直接访问请求(如上所述)。例如:

# 0. Ignore static resources
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^(weekend\.html|we-are-closed-come-back-later\.html)?$ - [L]

重写(内部)而不是重定向(外部)

,您可以内部重写URL而不是重定向。然后,您只有一个公共URL(即文档root)和基础文件(即Weekend.htmlWe-are-close-closed-come-come-back-later.html )可以从用户隐藏。

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".

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:

# 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]

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 or we-are-closed-come-back-later.html) can be hidden from the user.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文