在 apache2 中配置使用 ip 地址重定向

在 apache2 中配置使用 ip 地址重定向

我不是配置 Apache 的专家,但我有一个需要解决的问题。下面是我的虚拟主机 Apache 配置,顺便说一下,它运行良好。问题是谷歌搜索不知何故显示我的 IP 地址而不是我的域名。

<virtualHost somesite.com:80>
        ServerName www.somesite.com
        RewriteEngine On
        RewriteCond %{HTTPS} off
        RewriteCond %{QUERY_STRING} "fbclid=" [NC]
        RewriteRule (.*) /$1? [R=301,L]
        RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

所以现在我知道我需要重定向我的 IP 地址,所以我使用了下面的解决方案,但这似乎对我不起作用。此外,HTTP 站点无法打开,我已附上屏幕截图。但是我的站点的安全版本正在运行。我想要的只是一个将我的 IP 地址重定向到域名的解决方案,这意味着当用户在 Google 搜索中单击 IP 地址时,它应该打开安全的 HTTPS 域名。请帮帮我。

RewriteCond %{HTTP_HOST} ^111\.11\.111\.11$
RewriteRule (.*) https://www.somesite.com$1 [R=301,L]

以下是打开我的网站基于 HTTP 版本的错误截图

在此处输入图片描述

答案1

您的配置中有三个重写规则:

# Rule 1
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} "fbclid=" [NC]
RewriteRule (.*) /$1? [R=301,L]
# Rule 2
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Rule 3
RewriteCond %{HTTP_HOST} ^111\.11\.111\.11$
RewriteRule (.*) https://www.somesite.com$1 [R=301,L]

由于它们是按顺序应用的,并且规则 2 没有条件,因此它会覆盖规则 3。更改规则的顺序:

# Rule 1
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} "fbclid=" [NC]
RewriteRule (.*) /$1? [R=301,L]
# Rule 3
RewriteCond %{HTTP_HOST} ^111\.11\.111\.11$
RewriteRule (.*) https://www.somesite.com$1 [R=301,L]
# Rule 2
RewriteRule ^(.*) https://%{HTTP_HOST}%{REQUEST_URI}

相关内容