1

I have installed my new website on an AWS EC2 instance and have an elastic IP. I have already enabled HTTPS for my site. At present, the domain loads with the website without any issue, but the IP points to the Apache default page. I followed several tutorials to point the IP address back to the HTTPS version of my site. But it's not working. But if I use https://xx.xx.xx.xx I get a "Your connection is not private" warning.

<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>
Praveen
  • 21
  • 4
  • _“but the IP points to the Apache default page”_ - that likely means, Apache does not know which virtual host to route the request to in the first place - and that means, it does not go into the directory where your .htaccess is located either. – CBroe Jun 10 '21 at 10:14
  • @CBroe Only IP points to the default apache page. Can you help me to troubleshoot? – Praveen Jun 10 '21 at 10:24
  • _“Only IP points to the default apache page”_ - yes, as I said, probably _because_ your VHost setup only covers a specific host name. – CBroe Jun 10 '21 at 10:27
  • @CBroe I have updated the post with my host file. As you can see, I am using in my host file. How to include IP to host file. – Praveen Jun 10 '21 at 10:39
  • @Praveen You got error for insecure connection because the SSL certificate name is not valid for IP address. This is normal. But I don't undestand the question: do you need to redirect to FQDN if a user visit your site with IP (https://xx.xx.xx.xx -> https://mynewwebsite.com) ? – Blackat.net Jun 10 '21 at 10:45
  • Check if any of this helps, https://stackoverflow.com/questions/5427379/apache-default-virtualhost – CBroe Jun 10 '21 at 10:49
  • @Blackat.net Yes, When a user visits the IP, they have to be redirected to FQDN. – Praveen Jun 10 '21 at 10:51

2 Answers2

1

You have to define two VirtualHost with 443 ports. One of this contains the same configuration for your application:

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

One for redirect without ServerName and ServerAlias equal to wildcard (*).

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

This prevent to get the default page even if the user try to make a request with a FQDN different from your configuration.

Important!: You have to respect the order of configuration. Salvo.

Blackat.net
  • 132
  • 1
  • 11
1

The problem solved when I replaced ServerName with my IP instead of my server FQDN. I assume this method only works, if you have added the server and domain in /etc/hosts, which I have already added.

    <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>
Praveen
  • 21
  • 4