skip to Main Content

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 [email protected]
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>

2

Answers


  1. Chosen as BEST ANSWER

    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 [email protected]
        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 [email protected]
        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. 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 [email protected]
    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 [email protected]
    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search