将 HTTP 和 HTTPS 请求重定向到临时页面 - https:// 不是重定向

Redirecting HTTP and HTTPS requests to a temporary page - https:// is not redirecting

我希望将所有 HTTP 和 HTTPS 请求重定向到 https://example.com/coming-soon.html

这是我试过的最后一个 .htaccess 文件:

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit


Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/coming-soon.html [L,R=302]

RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests

我对 总是 将 HTTP 重定向到 HTTPS 感兴趣,这似乎已经有效,但与此同时我需要将整个域重定向到 /coming-soon.html,因为该网站还没有准备好。使用上面的 .htaccess,HTTPS 请求不会重定向到 coming-soon.html

之前,我有另一个 .htaccess 文件:

# php -- BEGIN cPanel-generated handler, do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
  AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler, do not edit


Options +FollowSymlinks
RewriteEngine on

RewriteCond %{HTTPS} !=on
RewriteCond %{REMOTE_ADDR} !^123.45.67.89$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !coming-soon.html

RewriteRule ^(.*)$ /coming-soon.html [R=302,L]

RewriteOptions inherit
Header set content-Security-Policy: upgrade-insecure-requests

使用此 .htaccess,重定向正在进行并且 HTTPS 工作,但如果我键入 https://example.com,它会将我带到 php 中站点文件名的根目录。

我已经阅读了很多链接,但实际上我要么理解不正确,要么做错了。我知道 IP 的 RewriteCond 尚未将我的 IP 设置为重定向的例外。暂时不用IP例外也行。

要将 http(s)://example.com/anything 重定向到 https://example.com/coming-soon.html,给定 IP 地址除外,请尝试这些 .htaccess 指令:

# Tested on Debian 10 running Apache 2.4.38
RewriteCond %{REMOTE_ADDR} !=192.168.001.001
RewriteCond %{HTTPS} off [OR]
RewriteCond %{REQUEST_FILENAME} !/coming-soon\.html$
RewriteRule ^ https://%{HTTP_HOST}/coming-soon.html [R,L]

其实只有https://example.com/coming-soon.html不会被重定向;我们不想开始循环。

在您发布的 .htaccess 个指令中,这些行:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/coming-soon.html [L,R=302]

不会产生预期的 https://example.com/anythinghttps://example.com/coming-soon.html 的重定向,因为单独的 RewriteCond %{HTTPS} off 不匹配 HTTPS 请求。添加 [OR] 和备用 RewriteCond 使 RewriteCond %{HTTPS} off 成为可选。


/coming-soon.html 包含分组在 /images 目录中的图像的情况下,.htaccess 指令可以是:

# Tested on Debian 10 running Apache 2.4.38

# Redirect all HTTP requests to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/ [R,L]

# Except for a given IP address,
# and for '/coming-soon.html' itself
# and for files within '/images/',
# redirect all requests to '/coming-soon.html'.
RewriteCond %{REMOTE_ADDR} !=192.168.001.001
RewriteCond %{REQUEST_FILENAME} !/coming-soon\.html$
RewriteCond %{REQUEST_URI} !^/images/
RewriteRule ^ https://%{HTTP_HOST}/coming-soon.html [R,L]