How to redirect https://ip to https://domain

July 27, 2019 200 views
Apache Server Optimization Ubuntu 18.04

The following lines are in the .htaccess I wrote.

<IfModule mod_rewrite.c>  
RewriteEngine On
RewriteCond %{HTTP_HOST} ^40.230.60.270
RewriteRule (.*) https://www.mywebsite.com/$1 [R=301,L]
</IfModule>

It redirects when I type this in the browser. http://40.230.60.270

But when I write like this, it doesn't redirect. https://40.230.60.270

I bought SSL certificate for two years and there was no problem with the certificate.

Can anyone tell me what its rewrite condition is ?

1 Answer
alexgeorgiev July 29, 2019
Accepted Answer

Hello,

Do you have a VirtualHost entry matching the IP address 40.230.60.270 with https and not http? If there is an entry for the IP address with https this will be the reason.

Create a VirtualHost entry matching 40.230.60.270 as ServerName, but not using https, but http instead. Then redirect this to your domain name.

Let me know how it goes.

Alex

  • HTTP Virtual Host with ServerName: mywebsite.com

    HTTPS Virtual Host on ServerName: mywebsite.com

    Are you asking for an IP address on HTTPS Virtual Host ?

    There is no IP address given to my virtual hosts.

    HTTPS will not access any HTTP virtual hosts.

    Do you mean that the problem is in the ServerName ?

    Full HTTPS Virtual Host Write it down pls, so what would that be like.

    • Thanks for confirming this.

      I just wanted to ask if everything is fine with the virtual host configuration.

      Also I have a question, when you access https:/40.230.60.270 you should see a certificate warning page and then you should be redirected to the site itself. You might have already acknowledged the certificate warning, but this is something that will happen for sure.

      Alex

      edited by MattIPv4
      • Yes you are right.

        There it says "Your connection is not private - NET::ERRCERTCOMMONNAMEINVALID"

        Do you think that HTTPS is never redirected ?

        Or it would have redirected but it is not being redirected because of the certificate ?

        Is it stuck on the certificate ?

        Or the virtual host is fine, Rather, it should be redirected from .htaccess

        • This is the normal behavior of a web server, because there is no SSL Certificate installed for the IP address itself and the browser will always give you this warning message regarding the certificate. It's just how the web server works.

          Let me know if you have any questions.

          Alex

          edited by MattIPv4
Have another answer? Share your knowledge.