apache - 将 HTTP 和 HTTPS 请求重定向到临时页面 - https ://is not redirecting

标签 apache .htaccess redirect

我希望将所有 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 设置为重定向的异常(exception)。我可以暂时不用IP异常(exception)。

最佳答案

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}/$1 [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]

关于apache - 将 HTTP 和 HTTPS 请求重定向到临时页面 - https ://is not redirecting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66933561/

相关文章:

amazon-web-services - 我的主页不会通过 HTTPS 端口

php - apache .htaccess 到 nginx 重写规则

php - 菜鸟 URL 重写

java - 在 Netty 中处理 Http 状态码 302 Moved Temporarily

java - 从Java更改为Android的空指针异常

apache - 如何在不显示8080端口的情况下在tomcat上部署java web应用程序?

sql-server - PHP警告: PHP Startup: Unable to load dynamic library 'C:\\Users\\Public\\xampp\\php\\ext\\php_sqlsrv_56_ts.dll'

php - 子目录中的 Wordpress 克隆

ubuntu - 如何使用 ssl Apache 将 www.subdomain 重定向到子域

asp.net - 在 C# 中,如何将用户重定向到访问被拒绝错误的 aspx 页面?