I configured a Wordpress site in LAMP (only one site under /var/www/), including an ssl certificate, the domain works fine, with no cert issues,however, if i reach the website using its public IP I get an error for the cert:
Your connection is not private
NET::ERR_CERT_COMMON_NAME_INVALID
which makes sense, since the cert is generated for the domain name and not the IP. I tried adding the following rule using my public IP and domain name as suggested in other posts to force a redirect from IP to domain name within the .htaccess file but it did not help:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xx.xx.xx.xxx$ [OR]
RewriteRule (.*)$ http://example.com/$1 [R=301,L]
I also tried the same on two virtual host files one on port 80 and 443:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xx\.xx\.xx\.xxx$ [OR]
RewriteRule (.*)$ http://example.com/$1 [R=301,L]
Any other suggestions ?
I finally found a solution, I used the following re-write condition in my htaccess file adding the IP :xx.xx..., and restarted apache:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^xx\.xx\.xx\.xx\.com [NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
I hope this helps future people who experience the same issue.