首页 > 解决方案 > 将 IP 地址重定向到域 (Apache)

问题描述

我已经在 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>

标签: wordpressapache.htaccess

解决方案


当我用我的 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>

推荐阅读