将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

Vhost:

<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>

您必须定义两个具有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发出请求,也无法获取默认页面。

重要:你必须尊重配置的顺序。萨尔沃。

当我将ServerName替换为我的IP而不是我的服务器FQDN时,问题得到了解决。我认为只有当您在/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>

最新更新