【问题标题】:Redirect IP address to domain (Apache)将 IP 地址重定向到域 (Apache)
【发布时间】:2021-08-27 07:40:18
【问题描述】:

我已在 AWS EC2 实例上安装了我的新网站并拥有弹性 IP。我已经为我的网站启用了 HTTPS。目前,域名与网站一起加载没有任何问题,但 IP 指向 Apache 默认页面。我按照几个教程将 IP 地址指向我网站的 HTTPS 版本。但它不起作用。但是,如果我使用 https://xx.xx.xx.xx,我会收到“您的连接不是私密的”警告。

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /

RewriteCond %{REMOTE_ADDR} ^xx\.xx\.xx\.xx$
RewriteRule ^(.*)$ https://mynewwebsite.com/$1 [L,R=301]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

虚拟主机:

<IfModule mod_ssl.c>
<VirtualHost *:443>

ServerAdmin admin@mynewwebsite.com
ServerName mynewwebsite.com
ServerAlias www.mynewwebsite.com
DocumentRoot /var/www/html/wordpress

ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined

Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/letsencrypt/live/mynewwebsite.com/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/mynewwebsite.com/privkey.pem

</VirtualHost>
</IfModule>

【问题讨论】:

  • “但 IP 指向 Apache 默认页面” - 这可能意味着,Apache 首先不知道将请求路由到哪个虚拟主机 - 并且意味着,它也不会进入您的 .htaccess 所在的目录。
  • @CBroe 只有 IP 指向默认的 apache 页面。你能帮我解决问题吗?
  • “只有 IP 指向默认的 apache 页面” - 是的,正如我所说,可能因为您的 VHost 设置仅涵盖特定的主机名.
  • @CBroe 我已经用我的主机文件更新了帖子。如您所见,我在主机文件中使用 。如何将 IP 包含到主机文件中。
  • @Praveen 您收到不安全连接错误,因为 SSL 证书名称对于 IP 地址无效。这个是正常的。但我不明白这个问题:如果用户使用 IP (xx.xx.xx.xx -> mynewwebsite.com) 访问您的网站,您是否需要重定向到 FQDN?

标签: wordpress apache .htaccess


【解决方案1】:

当我用我的 IP 而不是我的服务器 FQDN 替换 ServerName 时,问题解决了。我假设这种方法只适用于我已经添加的 /etc/hosts 中的服务器和域。

    <VirtualHost *:80>
    
    ServerAdmin admin@mynewwebsite.com
    ServerName xx.xx.xx.xx       <------------------- IP
    ServerAlias www.mynewwebsite.com
    DocumentRoot /var/www/html/wordpress
    
    ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
    CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
    
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mynewwebsite.com [OR]
    RewriteCond %{SERVER_NAME} =www.mynewwebsite.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    </VirtualHost>

    <VirtualHost *:80>
    
    ServerAdmin admin@mynewwebsite.com
    ServerName mynewwebsite.com       <------------------- Domain
    ServerAlias www.mynewwebsite.com
    DocumentRoot /var/www/html/wordpress
    
    ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
    CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
    
    RewriteEngine on
    RewriteCond %{SERVER_NAME} =mynewwebsite.com [OR]
    RewriteCond %{SERVER_NAME} =www.mynewwebsite.com
    RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

    </VirtualHost>

【讨论】:

    【解决方案2】:

    您必须定义两个具有 443 端口的 VirtualHost。 其中之一包含与您的应用程序相同的配置:

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
    
    ServerAdmin admin@mynewwebsite.com
    ServerName mynewwebsite.com
    ServerAlias www.mynewwebsite.com
    DocumentRoot /var/www/html/wordpress
    
    ErrorLog ${APACHE_LOG_DIR}/mynewwebsite.com_error.log
    CustomLog ${APACHE_LOG_DIR}/mynewwebsite.com_access.log combined
    
    Include /etc/letsencrypt/options-ssl-apache.conf
    SSLCertificateFile /etc/letsencrypt/live/mynewwebsite.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/mynewwebsite.com/privkey.pem
    
    </VirtualHost>
    </IfModule>
    

    一个用于不带ServerNameServerAlias 的重定向,等于通配符(*)。

    <IfModule mod_ssl.c>
    <VirtualHost *:443>
    
    ServerAdmin admin@mynewwebsite.com
    ServerAlias *
    DocumentRoot /var/www/html/
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteRule ^(.*)$ https://mynewwebsite.com/$1 [L,R=301]
    </IfModule>
    
    </VirtualHost>
    </IfModule>
    

    即使用户尝试使用与您的配置不同的 FQDN 发出请求,这也会阻止获取默认页面。

    重要!:你必须尊重配置的顺序。 齐射。

    【讨论】:

    • 我测试了上面的配置,但没有结果。它破坏了网站。
    • @Praveen 我进行了更改,并在我的办公桌上进行了测试。现在它起作用了。对不起:P
    • 没关系。我已经备份了配置。这没什么大不了的。
    • 为什么是 443?它必须是端口80?我已经解决了这个问题。现在一切正常。请参阅我的解决方案。看看有没有问题?
    • @Praveen 完美! :) 拜托,你能把我的帖子标记为答案吗?
    猜你喜欢
    • 1970-01-01
    • 2016-01-09
    • 2020-04-07
    • 2011-01-23
    • 2021-03-25
    • 2021-11-23
    • 2014-05-20
    • 2023-01-27
    相关资源
    最近更新 更多